I'm a software engineer with over 15 years of experience developing web and mobile applications. Follow my blog, and follow me on Pluralsight https://www.pluralsight.com/authors/marques-woodson
Here's an example of using the React.useReducer hook in the UserComponent defined in the article:
functionUserComponent(){// userReducer is defined above in the article.const[state,dispatch]=React.useReducer(userReducer,initialState);const[input,setInput]=React.useState('');constupdateInput=(input:string)=>{setInput(input);};constupdateName=()=>{dispatch({type:'SET_NAME',payload:input,});};return(<div><h1>Mynameis:{state.name}</h1>
<inputvalue={input}onChange={e=>updateInput(e.target.value)}placeholder="Enter name here"/><buttononClick={updateName}>Submit</button>
...</div>
);}
I am using the useReducer hook to create state and dispatch variables. In the userReducer switch statement i have a SET_NAME case, which should just update the name property. We do that by dispatching an action of type SET_NAME, and with a payload of the new name. This happens in the above updateName method.
Does this make more sense?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's an example of using the React.useReducer hook in the
UserComponent
defined in the article:I am using the
useReducer
hook to create state and dispatch variables. In theuserReducer
switch statement i have aSET_NAME
case, which should just update the name property. We do that by dispatching an action of typeSET_NAME
, and with a payload of the new name. This happens in the aboveupdateName
method.Does this make more sense?