score:3
The 'react-redux'
library contains binding methods between React and Redux. If you haven't done so already, I really recommend checking out
Dan Abramov's: 'Getting into Redux' series of videos.
He goes into a good amount of detail about how to build a working Redux application from scratch and then how to do the same in conjunction with React (again from scratch).
He finalises on the use of the 'react-redux'
helper library to make wiring up React with Redux easier.
The resulting solution for you would be to:
Use the
connect
method in Redux to create a Redux container component (just a term for a React component with Redux bindings)mapStateToProps
receives updates on the current state of the store which you can map to the target componentsprops
. Yo'd use this to get the current state of the store for use in your componentmapDispatchToProps
which gets the store's dispatch action which you can use to bind action creators to (to update the store). You'd use this to connect the action creators that update the state of your store.
score:2
I assume you've already setup your reducers and actions. Now the only thing you need to do is dispatch an action from your LocalComponent
.
Lets say you've a method called toggleTableState
for updating the state tableBusy
of your LocalComponent
.
LocalComponent.js
import React, { PureComponent, PropTypes } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions`;
@connect(
(state, props) => ({
isTableBusy: state.isTableBusy,
}),
{
toggleTableGlobalState: actions.toggleTableGlobalState,
})
export default class LocalComponent extends PureComponent {
static propTypes = {
toggleTableGlobalState: PropTypes.func,
isTableBusy: PropTypes.bool,
}
...
toggleTableState() {
this.setState({ tableBusy: !this.state.tableBusy });
this.props.toggleTableGlobalState(!this.state.tableBusy);
}
...
}
actions.js
export const TOGGLE_TABLE_STATE = 'TOGGLE_TABLE_STATE';
export function toggleTableGlobalState(tableState) {
return { type: TOGGLE_TABLE_STATE, tableState };
}
reducer.js
import { TOGGLE_TABLE_STATE } from './actions';
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
...
case TOGGLE_TABLE_STATE:
return { ...state, isTableBusy: action.tableState };
break;
...
}
}
Source: stackoverflow.com
Related Query
- How can I store and subscribe component state to redux store?
- How to use useEffect hook properly with array dependency. I passed state from redux store and still my component renders infinitely
- how does react props merge state passed from parent component and redux store
- How to use React Redux store in component that modifies and renders the state
- Jest/Enzyme Unit Testing: How to pass store to shallow component that uses redux 4 and react-redux 6 connect function
- Should I use redux-form store instead of component state and Redux custom store?
- how and when to call a react component methods after state change from redux
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How can I initialize Redux state for React component on its creation?
- How to subscribe to state outside React component in Redux Toolkit?
- How React Testing Library can assert children props and Redux store updates?
- How can i pass a component ref to redux store
- How to store react component in state and pass a callback function
- How can I update state in Redux - moving todo up and down - React
- How can I redirect to another component in react and pass the state that I set in the previous component?
- Redux how to get updated store state in root component
- How to get updated state from Redux store using redux-toolkit after component has already rendered?
- How do I stop state store data from accumulating in a redux component every time I navigate to it using react router
- How can I store all the dynamic values and attribute labels in a state in react js
- Async props pass in React: How to pass a key to update redux store in react? [First from the higher component and then from redux store]
- How can I pass the state from one component to another and how can I modify the state?
- How and where call function in component conditionally based on redux state
- How can I maintain state and "this" with react component functions
- How to save fetched data from server to component state using redux and redux-thunk?
- How can I keep previous state and add a new state to the previous state in Redux reducers?
- React Redux - How to store form state so that it can be populated again when user returns to page
- How can I pass state from page to component and back to page?
- How can i get values from multiple input boxes on form submit and store the values as an object in an state array for React?
- How to store API data in component state and display it
- How can I use init state without using Provider and connect() in Redux
More Query from same tag
- Pass input values with React-Final-Form to Axios POST
- How to center Items in react
- Problem using multiple Reducers and Actions Redux
- adding multiple items to Redux store
- .child is not a functions
- Handling subscription/unsubscription to a ton of events with redux-saga
- Correct way to map JSON to Table by Fetch API method (react js)
- Is there way to get return value from reducer action? (in React-Native)
- How to make browserHistory work with webpack react
- HTTPS on localhost using NextJS + Express
- how to render react component to another web app
- How to handle data binding in Rails while using React.js for the front end?
- How to fetch just one time ( stop fetching data after one is fetched )
- why are the routes undefined
- Webpack+ tsconfig + dynamic import throwing NoEmit error for .d.ts files
- Connection with Basic HTTP authorization with email and password body application/x-www-form-urlencoded with axios
- Issue clearing a recursive timeout with onClick in React
- How to make page to the top when route changes without scrolling in Reactjs
- Event Emitter Pattern for RecoilJS
- React: TypeError: .map is not a function
- Cannot post image file from React frontend to express and MySQL
- React 16 Radio Button onChange not working
- Cannot import firebase into react.js project. "Module not found: Error: Package path . is not exported from package"
- React Highcharts 'forExport' of undefined
- How to explicitly create an optional property in a React component when it is not specificied?
- Media query condition styled-components
- ReduxToolkit useSelector Hook: React functional component doesnt rerender after selected redux-state is updated through useDispatch
- Debugging fetch operations in next.js
- how to prevent duplicate data from react
- Why we need "value" prop of RadioGroup in react?