Created
May 4, 2021 21:23
-
-
Save NathanLeadill/7e177212da4879fad384730e42501864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initial = { | |
firstName: '', | |
lastName: '', | |
info: '' | |
}; | |
function exampleReducer(state, action) { | |
switch (action.type) { | |
case 'update_firstName': | |
return { | |
...state, | |
firstName: action.value, | |
}; | |
case 'update_lastName': | |
return { | |
...state, | |
lastName: action.value, | |
}; | |
case 'update_info': | |
return { | |
...state, | |
info: action.value, | |
}; | |
default: | |
return { | |
...state, | |
}; | |
} | |
} | |
export function ComponentOne() { | |
const [state, reactDispatch] = useReducer(exampleReducer, initial); | |
return ( | |
<> | |
<div id="inputcontainer"> | |
<label>First Name</label> | |
<input | |
label="firstName" | |
onChange={(e) => { reactDispatch({ type: 'update_firstName', value: e.target.value }); }} | |
/> | |
</div> | |
<div id="inputcontainer"> | |
<label>First Name</label> | |
<input | |
label="firstName" | |
onChange={(e) => { reactDispatch({ type: 'update_lastName', value: e.target.value }); }} | |
/> | |
</div> | |
<div id="inputcontainer"> | |
<label>First Name</label> | |
<input | |
label="firstName" | |
onChange={(e) => { reactDispatch({ type: 'update_info', value: e.target.value }); }} | |
/> | |
</div> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment