in Education by
According to React docs : useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. 1. can somebody explain me why useReducer is not updating the state synchronously ? const reducer = (state, action) => { if( action.type === 'ADD_VALUE') { console.log(`STATE IN REDUCER`, [...state, action.path]) // => ["1.1"] return [...state, action.path] } } const [state, dispatch] = useReducer(reducer, []) { dispatch({ type: 'ADD_VALUE', path: "1.1"}) console.log(`STATE`, state) // => [] // here i want to do some stuff based on the lastest updated state (["1.1"] and not []) // for example dispatch an action with redux }}/> 2. How can I do some stuff (dispatch a redux action) based on the lastest updated state (["1.1"] and not []) ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Use useEffect to access the state correctly. You could add some safe-guarding if you want something invoking if a certain criterion is hit. If you want to access your reducer across components, you can store the reducer using Context API. Look below for an example. You can see the reducer being injected into the Context on the parent component and then two child components that a) dispatches an action b) receives the update from the action. 1. Example of context reducer to use across multiple components import React from "react"; import ReactDOM from "react-dom"; const Application = React.createContext({ state: null, dispatch: null }); function ActionComponent() { const { dispatch } = React.useContext(Application); return (
Action Component
); } function ListenerComponent() { const { state } = React.useContext(Application); React.useEffect( () => { console.log(state); }, [state] ); return
Listener Component
; } function App() { const [state, dispatch] = React.useReducer(function(state = [], action) { return [...state, action]; }); return (
); } const rootElement = document.getElementById("root"); ReactDOM.render(, rootElement); 2. Example of local reducer without using Application Context const reducer = (state, action) => { if( action.type === 'ADD_VALUE') { return [...state, action.path] } } const [state, dispatch] = useReducer(reducer, []) React.useEffect(() => { console.log(state); }, [state]); { dispatch({ type: 'ADD_VALUE', path: "1.1"}) }}/>

Related questions

0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    How can I get the td values with jquery? The while loops will generate a few with the same class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    My requirement is once I click on checkbox and save/update textbox should be disabled and when I uncheck ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    My requirement is once I click on checkbox and save/update textbox should be disabled and when I uncheck ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    How can I check a regular expression to be fulfilled only if there is a number or group of numbers ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    How can I check a regular expression to be fulfilled only if there is a number or group of numbers ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to access the correct `this` inside a callback (13 answers) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
...