Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
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:
import React from 'react'; import ReactDOM from 'react-dom'; import { createStore } from 'redux'; var store = createStore(changeState); 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; } } class Home extends React.Component { render() { return ( <React.Fragment> <Comp1 /><br /> <Comp2 /> React.Fragment> ) } } class Comp1 extends React.Component { increase = () => { var action = { type: 'INCREMENT', qty: 1, price: 100 } store.dispatch(action); } render() { return ( <button type="button" onClick={this.increase}>Increasebutton> ) } } class Comp2 extends React.Component { constructor() { super(); this.state = { cartQty: 0, cartPrice: 0 } } render() { store.subscribe(() => { var globalState = store.getState(); REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
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
store
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
mapStateToProps
mapDispatchToProps
connect
Fifth Create store after defining changeState reducer and initialState
import React from 'react'; import ReactDOM 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); class Comp1 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> ) } } const Comp1Container = connect()(Comp1); class Comp2 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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.