Comments (2)
Hey, basically when you use react-router
and you have a route like /users/:id
, where id
can change, you want to fetch the new user every time the url param changes.
So you need something like componentDidUpdate
which checks if props
changed and eventually fetch new data. So you would need that for each component that does data fetching.
The decorators basically abstract this logic allowing you to just specify in a clean and nice way the params
that you are interested in and what kind of actions you want to trigger.
@fetchOnUpdate([ 'username', 'repo' ], (params, actions) => {
const { username, repo } = params
actions.fetchRepo({ username, repo })
actions.fetchRepoStargazers({ username, repo })
})
This means:
- check
username
andrepo
params and get their values fromthis.props.params
(RR) - call the callback function with those
params
and theactions
which were passed from the smart component - whenever
params
change the callback function is called, causing new data to be fetched
Hope this answer your question :)
from redux-react-router-async-example.
you want to fetch the new user every time the url param changes.
I've just added Redux to my application and moved my data fetching logic from componentDidMount/componentWillReceiveProps to a Redux action. I've been researching the best to dispatch actions when the URL updates from navigation/someone deep linking.
First I used react-router-redux to listen to history changes, and then dispatch actions based on the URL using a switch.
This seems to work, and I really like that all data updates live outside of the react components. This way the components are never required to initiate data fetching; it's all handled by Redux. This feels like a good separation of concern. My view layer doesn't have to worry about dispatching anything when the URL changes.
After reading that react-router-redux was only intended to ensure URL updates are included in the Redux store for Time Travel via DevTools, I decided to do some more research.
This is probably my favourite solution so far (storing URL in a Redux store and observing changes using RxJS):
reduxjs/redux#227 (comment)
I'm interested if you would still use your fetchOnUpdate HOC solution today, or have you found any alternative solutions to this problem that you prefer?
from redux-react-router-async-example.
Related Issues (20)
- Support direct access to login route
- domo does not work HOT 1
- Is there any good way to apply translations xml file to original json? HOT 2
- Don't work on Safari HOT 2
- This is more of a full boilerplate project example than a redux-router example HOT 6
- How to start with devtools? HOT 3
- IE support ? HOT 2
- import * sentence for language files HOT 4
- How do you switch the language on runtime?
- actionhandlers HOT 2
- Force childs to wait for data updates HOT 3
- Error using Material UI Components HOT 1
- React-intl, locale stored on LocalStorage HOT 2
- reducer is not a function
- Tests
- Question about your XLIFF workflow HOT 1
- Redux Router VS React Router Redux HOT 7
- Strange issue with logger middleware HOT 1
- Is it Really Async ? HOT 1
- How can I remove router hash? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-react-router-async-example.