Implementing form in React, pre and post hooks
Prior to hooks, we can keep the values in state and set it as such:
onClick = (event) => {this.setState({ [event.target.name]: event.target.value});}
How are we going to do it with hooks?
const reducer = (previousState = {}, updatedState = {}) => {return {...previousState, ...updatedState};};const useSetState = (initialState = {}) => {const [state, dispatch] = useReducer(reducer, initialState);const setState = updatedState => dispatch(updatedState);return [state, setState];}export default useSetState;
In the component:
const [state, setState] = useSetState(initialState);const handleChange = (event) => {setState({[event.target.name]: event.target.value});}