I'm using React 16.6 and react-router v4 for a master-detail web application. I'm showing headers in a navbar, with individual elements rendered using React Router and Links. Using components I can easily render array elements, but I want to allow in-place editing for each component with state changes propagated back to the parent.
Following details here, I've tried passing a callback function using state in the "to" object. The function isn't available when I inspect the component constructor.
Example
<Link class="col" to={{pathname: `/listing/${id}`, state: {entity: elem, callback: updateComponent}}}>{elem['name']}</Link>
Where updateComponent is a function.
What's the preferred way to approach this scenario?
Just glanced it over and from what I gather the action is for the History.
action - (string) The current action (PUSH, REPLACE, or POP)
You could trigger callbacks with onClick
Related
Is there a way to use the history hook function goBack(), in the same way, we use the Link component?
<Link to={{pathname:'/brand', state: {id}} />
Let's say I want the user to be able to go back to a previous page with an indexed table, to a precise table page. so I will first pass the index value in the Link state component, then on the way back using goBack() is there a way to pass again the state in order to redirect to that specific index?
hope it's clearer now
I'm Using react-router v4
How can I effectively measure (via navigation timing performance.mark and performance.measure) how long it takes for a user from the click event on the link until the updated / rendered route he navigated to?
How exactly are you defining your routes?
If you are utilizing <Link/> components, you could register your performance mark in an onClick handler as a side-effect. For example, something like the following:
<Link
to="some/great/component"
onClick={() => performance.mark('initialize_page_change')}
/>
Then, in the component that will render as a result of the route-change, you'll want to make a call to performance.mark again. You'll probably want to make use of React's lifecycle methods for this purpose. I'd recommend using the constructor function or componentWillMount(). In your component that corresponds to the above link:
componentWillMount() {
performance.mark('target_page_mounted');
}
And finally, to measure your marks:
performance.measure('reactRouterPerf',
'initialize_page_change',
'target_page_mounted'
);
The use case is that I want to map the root (/) to one of two different components based on whether the user is logged in or not, and I want these two components to reside in different bundles and lazily loaded, so simply putting the login check in the render() method would not do.
I tried to use dynamic route definition with require.ensure() to lazily load the component, and it works for the first time, but after changing the login state the component doesn't get updated (even if I navigate to another route and back to / ).
I tried to force re-rendering the router by setting props on the component that contains the router, both manually and by making it a Redux connected component, and I also tried to add a listener to the Redux store and change the component state in response to login change, but in all of the attempts I got the error "You cannot change ; it will be ignored" and the component doesn't change.
My ugly solution is to have the different component loading code outside of the router, listen to the login state change and in response load the matching component and set it in the wrapping component's state, which is referenced in the render() code. Is there a clean "React-Router-ish" way to do what I want?
React Router 4 pretty much solves this as it made the route configuration part of the component rendering, so having conditional rendering is the same whether it's based on the location or on other props/state.
The closest thing to a clean "React-Router-ish" way to do that is to use the React Router Enterhooks.
An enter hook is a user-defined function that is called when a route is about to be rendered. It receives the next router state as its first argument. The replace function may be used to trigger a transition to a different URL.
So, use the onEnter(nextState, replace, callback?) attribute on your <Route />.
Called when a route is about to be entered. It provides the next router state and a function to redirect to another path. this will be the route instance that triggered the hook.
If callback is listed as a 3rd argument, this hook will run asynchronously, and the transition will block until callback is called.
The general best practice I follow is to place the auth-check flow away from your routes, and place it inside the transition events/hooks.
The usual behavior is - before the route handler actually gets rendered, check the auth, and redirect the user to another route. In your case, if you want to use the same route, but render different components - you should be able to do that using the same technique too. However, that's not a common thing (based on what I've seen), but it should be possible.
For a complete example of this approach, here's the auth-flow code example you can check. It is shared by the creators of React Router, so it looks credible to me.
PS: My answer is valid for React Router versions > 0.13.x.
I'm looking for solution for paging in routing with react-router and redux.
React-router don't fire callback in onEnter hook if only query changes, Router.run method is deprecated, so I'm a bit puzzled. Are there any other thing to do besides manually subscribing on location.change or use of react's lifecycle hooks like willReceiveProps?
Per the comments, the only hook left to you on the <Router> directly is onUpdate. You might also be able to intercept query parameters via a custom RoutingContext, but we don't currently consider that a public API.
We're looking to add a better solution for this use case in the future, but the approaches outlined are the only ones available for the 1.0.0 release.
For anyone using v2.0/v3.0, you can use the route's onChange hook to respond to query changes.
<Route
component={...}
path="..."
onChange={(nextState, replace, callback) => {
// Do something in response to a query change...
}}
/>
onChange(prevState, nextState, replace, callback?)
Called on routes when the location changes, but the route itself
neither enters or leaves. For example, this will be called when a
route's children change, or when the location query changes. It
provides the previous router state, the next router state, and a
function to redirect to another path. this will be the route instance that triggered the hook. If callback is listed as a 4th argument, this hook will run asynchronously, and the transition will block until callback is called.
https://github.com/remix-run/react-router/blob/v3/docs/API.md#onchangeprevstate-nextstate-replace-callback
In Backbone.js, you can specify a view's element by using the el property or by calling view.setElement().
Is there an equivalent way of hooking up a React.js component to an existing DOM element?
I'm not overly familiar with Backbone, but to hook a React component into the DOM you use the renderComponent function. The first arg is the component, and the second is a DOM element:
React.renderComponent(<SampleComponent />, document.getElementById('app'));
Updated per the context given in the comments:
React hooks up to an element by replacing its contents, but not the element itself. You can call renderComponent() more than once on that element and it will run the same diff algorithm each time. This is handy if you want to pass in different props, pre-render on the server, or render a different component entirely. The same process is used to update the actual DOM each time, just like if you were to use setState() within the component itself.