Skip to content

Instantly share code, notes, and snippets.

@msarsha
Created August 18, 2019 13:52
Show Gist options
  • Save msarsha/23e27f135922e9e9dab4978b1dea2aa3 to your computer and use it in GitHub Desktop.
Save msarsha/23e27f135922e9e9dab4978b1dea2aa3 to your computer and use it in GitHub Desktop.
react component
const userFormReducer = (state, action) => {
switch (action.type) {
case actionTypes.INPUT_CHANGE: {
return {
...state,
[action.name]: action.value
}
}
default:
return state;
}
};
const NewEditUserModal = (props) => {
const [state, dispatch] = useReducer(userFormReducer, props.user || {});
const handleInputChange = (name, value) => {
dispatch({type: actionTypes.INPUT_CHANGE, value, name})
};
return (
<React.Fragment>
<form>
<span>User Modal</span>
<label>
Full Name:
<input type="text" value={state.fullName} onChange={(e) => {
handleInputChange('fullName', e.target.value)
}}/>
</label>
<Button onClick={props.closeModal} type="button">Close</Button>
</form>
{state.fullName}
</React.Fragment>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment