User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
I am learning redux and I have put up a simple code which uses the store, action and reducer. I am using store.subscribe() to listen for changes in the state and using it to update my local state.
There are few things that you need to take care of.
First, you need to have a react-redux installed and then use Provider component at the top level to which you pass store
Second: You need to connect the components that need to access store or dispatch
Third: You need to render the connected components
Fourth: You need mapStateToProps to be passed to container which access state and mapDispatchToProps to accesses dispatch or make action creators available as props to components. In case you do not pass mapDispatchToProps to connect, by default dispatch prop is made available to the component. You can look at the API docs here
Fifth Create store after defining changeState reducer and initialState
importReact from 'react';importReactDOM from 'react-dom';import{ createStore } from 'redux';import{ connect,Provider} from 'react-redux'var initialState ={
qty:0,
price:0}function changeState(state = initialState, action){switch(action.type){case'INCREMENT':var stateCopy1 =Object.assign({}, state);
stateCopy1.qty = stateCopy1.qty + action.qty;
stateCopy1.price = stateCopy1.price + action.price;return stateCopy1;default:return state;}}var store = createStore(changeState);classComp1 extends React.Component{
increase =()=>{var action ={
type:'INCREMENT',
qty:1,
price:100}this.props.dispatch(action);}
render(){return(<button type="button" onClick={this.increase}>Increasebutton>)}}constComp1Container= connect()(Comp1);classComp2 extends React.Component{
render(){return(<div><h1>Total items in cart:{this.props.cartQty}h1><h1>T
There are few things that you need to take care of.
First, you need to have a react-redux installed and then use Provider component at the top level to which you pass store
Second: You need to connect the components that need to access store or dispatch
Third: You need to render the connected components
Fourth: You need mapStateToProps to be passed to container which access state and mapDispatchToProps to accesses dispatch or make action creators available as props to components. In case you do not pass mapDispatchToProps to connect, by default dispatch prop is made available to the component. You can look at the API docs here
Fifth Create store after defining changeState reducer and initialState
importReact from 'react';importReactDOM from 'react-dom';import{ createStore } from 'redux';import{ connect,Provider} from 'react-redux'var initialState ={
qty:0,
price:0}function changeState(state= initialState, action){switch(action.type){case'INCREMENT':varstateCopy1 =Object.assign({},state);stateCopy1.qty =stateCopy1.qty + action.qty;stateCopy1.price =stateCopy1.price + action.price;returnstateCopy1;default:returnstate;}}var store = createStore(changeState);classComp1 extends React.Component{
increase =()=>{var action ={
type:'INCREMENT',
qty:1,
price:100}this.props.dispatch(action);}
render(){return(<button type="button" onClick={this.increase}>Increasebutton>)}}constComp1Container= connect()(Comp1);classComp2 extends React.Component{
render(){return(<div><h1>Total items in cart:{this.props.cartQty}h1><h1>T
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that
you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice
sessions to improve your knowledge and scores.
manpreet
Best Answer
3 years ago
I am learning redux and I have put up a simple code which uses the store, action and reducer. I am using store.subscribe() to listen for changes in the state and using it to update my local state.
Below is my entire code in index.js: