score:13
Accepted answer
The most 'redux-like' way of handling the pre-loading of data would be to fire off the asynchronous action in the lifecycle method (probably componentWillMount
) of a Higher Order Component that wraps your app. However, you will not use the results of the API call directly in that component - it needs to be handled with a reducer that puts it into your app store. This will require you to use some sort of a thunk middleware to handle the asynchronous action. Then you will use mapStateToProps
to simply pass it down to the component that renders the data.
Higher Order Component:
const mapStateToProps = (state) => {
return {
departments: state.departments
};
}
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
getDepartments: actionCreators.fetchDepartments
});
}
class App extends Component {
componentWillMount() {
this.props.getDepartments();
}
render() {
return <DepartmentsList departments={this.props.departments} />
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
reducers:
export function departments(state = [], action) {
switch(action.type) {
case 'RECEIVE_DEPARTMENTS':
return action.departments;
}
}
Source: stackoverflow.com
Related Query
- Correct way to pre-load component data in react+redux
- Correct way to create a editable component fetching data from server?
- Correct place to load data in connected redux component
- react-apollo Query: Correct way to get the data from a <Query> Component
- What is the correct way of adding a dependency to react in your package.json for a react component
- Is there a right way to pass data into a React component from the HTML page?
- Correct way of Creating multiple stores with mobx and injecting it into to a Component - ReactJs
- TS2322 Error when extending Button, component property not found. What is the correct way to type?
- React Redux correct way to handle error / success messages unique to each component
- React - load all data from json into component
- What is the correct way of dispatching an action not tied to a component with Redux?
- Load the component when all data has ready
- ReactJS: Prettiest way to dynamically load Component within Modal
- ReactJS - load the data before rendering the component
- Getting error "Cannot update a component while rendering a different component". What is the correct way to achieve what I'm attempting?
- Correct way to call passport js function from react component
- Correct way to cancel async axios request in a React functional component
- ReactJS bind a component method the correct way
- Unmounting a React component the correct way
- Correct way to pass data in JSX clicks
- which is the correct way while using props in functional component in react
- React Router "Link to" does not load new data when called from inside the same component
- How to load initial data in Flux, but only if a component needs it
- Load and display json data in react component
- The correct way to unit test react component
- ReactJS - Proper way to fetch data from redux and load form
- Best way to store non-observable data in a React Stateless Component (with Hooks)
- Correct way to fetch data when switching route on same level in react-router?
- My component is animating after page load when the data is loaded in the useState
- What is the preferred way to pass data from a Rails view to a react component in Rails 6?
More Query from same tag
- Is it possible to dynamically render elements with props using TypeScript?
- Strapi register method not allowed 405 in v4 seems url is incorrect
- How to add insert objects by id
- React JSX inner loop rendering
- withFormik how to access wrapped form's props in handleSubmit
- Can't change class in React
- Render single icon on hover using map - ReactJs
- How to set useState for change view on reactJs
- How to reset a react-select single-input with Formik with reset button
- Deploying React App on IIS8 Windows server (Azure VM)
- What is the proper way to smoothly scale an element to nothing and then back to its original size
- How to use same lines of code in all components in react?
- Set initial state using local/session storage in react application
- react maps google outsie Grid
- Icons in react-table
- When I update the state variable of child component ,why it cause infinite loop?
- 'Property does not exist' in a Functional Component with added functional components as properties to it?
- How can I loop over a JSON object in React
- useState array becomes 2 arrays in console.log?
- Navigating to a 404 Route with Reach Router
- React Joyride-How can I adjust position of floater?
- how to make rounded RaisedButton and Textfield
- react error unknown : TypeError: Cannot read properties of undefined
- PhpStorm for React gives me a red underline
- Syntax Error React.js
- React fetch data in server before render
- I create a build of my react app and connected with electron.js. now how to connect my backend from server folder with electron
- Firebase React. How can I access my Auth values in React / Webpack?
- Sending Array of Objects to MongoDB Atlas using node.js
- How to add different background colours for each droppable element