I am using react router with flux. Is there any way to know the current route in an store? I tried using this.props.location.pathname but it is always undefined.
Since last version (v4) you can access to your app navigations state as simple as Actions.state, use Actions.currentScene to get name of current scene.
You can see more details on GitHub https://github.com/aksonov/react-native-router-flux
Related
I have a fully functional React web-app that now has a new requirement of loading separate images based on the URL suffix. It seems that in order to retrieve any information from the path I need to have a React Router App. Is there ---
A: A way to easily migrate to React Router so I can use this.props.location.pathname?
or B: Another way to retrieve the pathname without migrating to React Router.
Thanks.
Using react-router to do only that would be overkill. You can easily retrieve the url path with window.location.pathname. What you should is setup a listener on the location that sets the pathname to state and you can pass it down to the components that need it.
window.addEventListener('locationchange', function(){
console.log('location changed!');
}
I'm trying to detect when the page location changes in a react app using the Xamarin forms webview .Navigated event. However, it seems like it's only issued when I navigate to an external page, not within the react app, even though I'm using react router. In my browser, the url changes but the Xamarin webview doesn't register that change, even if I manually poll the url by printing webView.Source.
Is there some way I can detect a navigation within the react app from C#?
If you want a component to be location aware, you can wrap it with the HoC withRouter provided by React Router.
You can get access to the history object’s properties and the closest <Route>'s match via the withRouter higher-order component. withRouter will re-render its component every time the route changes with the same props as render props: { match, location, history }.
Once you're getting location change updates in your react app you can update your C# to perform any actions you require.
EDIT: To address your comment...
Now I've never used C#, but according to my research this should work.... take a look at __doPostBack. You can read about how to use it here.
In the React-Router documentation I have seen that It has import both Route and Router modules from react-router-dom. I would like to know what is the different between those two modules?
Router
Router component is what makes the connection between browser location and the react application. It doesn't render anything visible on your page. It just exposes API to interact with location changes of the browser via React context. So any component down the tree can use this API to change their behavior based on location changes in the browser or change the browser location into what they want.
Router is the abstract for all other specific router components. Practically, we use a specific implementation of it like BrowserRouter, MemoryRouter, and HashRouter which use different methods to manage browser history. Also, Router is usually a top-level component in the component tree and use only one time in the entire application. All other react-router components should be descendants of Router as they can't function without the API which Router provides.
Route
Route is much simple to explain. It just renders some UI when a location matches the route’s path. So an application can have many Routes based on its layout complexity in different levels of the component tree. Also, Route has some additional props to configure how the match should happen. Route internally use API provided by Router to access the location and decide whether to render the given component or not.
The question of the application architecture.
Suppose there are a lot of components (look at the picture) (mains) on a page.
What better use to switch the main children's components (active / not active)?
And pages (1, 2, 3, next)?
Can I use react-router for both tasks?
P.S.: I use ReactJS for rendering
I would not use react-router for results filtering. Since you are using Redux, you can split your reducers in to main sections and keep the state of active/not active as well as current page per section all in one store.
I think you should not need use react-router in this page.
You should control the state (active/not active,1,2,3,next) by reducer,just like below:
case NEXT_PAGE:
return {...state, list:[{active:0, ...others},{active:1, ...others},...], page: 1};
And you can get the data that you want to display on this page from props.
mapStateToProps(state) {
return {
list: state.xxx.list,
currentPage: state.xxx.page
totalPage: state.xxx.total
}
}
The key is the use case. If you have following use cases:
A user to copy paste the link of filtered result and page and share with others
A user a bookmark a filtered result page, and come back to it later.
Then definitely I would say use the react router.
You can have a route like this:
<Route path="main/:status/:page" component={Main}/>
Then the params object would be injected as props to your Main component.
If user click on the buttons, you would use the push from react-router to change the route or the simply Link from react router. (instead of using dispatch action with redux or using this.setState if you stored as local state).
I think it isn't more difficult than storing it with the redux or local state, with the additional advantage of more use cases above.
If you want a solid and scalable application architecture I'd suggest using a starterkit.
The wiki of a good starter kit will describe the chosen design principles.
One of my favourite starter kits to combine with a headless backend is React Redux starter kit.
So you want to do routing with your Redux app. You can use it with react-router-dom. react-redux will be the source of truth for your data and react-router-dom will be the source of truth for your URL.
Handle data with state management using react-redux (create actions, reducers ...) and navigation with react-router-dom.
Usage with React Router
I am pretty new to React-Router, I wonder if there is a simple way to change Route, some concept like state in Angular UI Router. From the React Router official tutorial :
https://github.com/reactjs/react-router-tutorial/tree/master/lessons/12-navigating
My understanding is: I have to manually change the url to trigger route change.
What exactly are you trying to achieve? Did you take at look at the react-router Link component?
Perhaps the most used component in your app is Link. Its almost identical to the <a/> tag you're used to except that its aware of the Router it was rendered in.
React-Router Tutorial / Lesson 3: Navigating with Link