Skip to content

Instantly share code, notes, and snippets.

@NathanLeadill
Created May 4, 2021 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanLeadill/7e177212da4879fad384730e42501864 to your computer and use it in GitHub Desktop.
Save NathanLeadill/7e177212da4879fad384730e42501864 to your computer and use it in GitHub Desktop.
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