I really like the new React hooks and I'm using them frequently for a project I'm working on. I'm coming across a situation where I want to use the prevState in the useState
hook, but I'm not really certain on how to do this.
I've tried something like this, but it fails to compile.
const [ someState, setSomeState ] = useState( new Map() ) setSomeState( prevState.someState.set( key, value ) )
(by the way, this is to map an array of checkboxes to keep track of the ones that are check marked)
I'm trying to follow this example here, but without using the setState
function.
Thanks for the help!
While there's currently no React Hook that does this out of the box, you can manually retrieve either the previous state or props from within a functional component by leveraging the useRef , useState , usePrevious , and useEffect Hooks in React.
For objects you can use the spread operator to use prevState
within your setState
call.
const [object, setObject] = useState({ firstKey: '', secondKey: '', }); setObject((prevState) => ({ ...prevState, secondKey: 'value', })); // object = { // firstKey: '', // secondKey: 'value', // }
The snippet below show an example of using prevState
for setting the state of an object.
const {useState} = React; const Example = ({title}) => { const initialState = { firstKey: 'empty', secondKey: 'empty', thirdKey: 'not empty', } const [object, setObject] = useState(initialState); const withPrevState = () => { setObject((prevState) => ({ ...prevState, secondKey: 'not empty', })); } return ( <div> <h5>Updates Second key to 'not empty'</h5> <p>First key: {object.firstKey}</p> <p>Second key: {object.secondKey}</p> <p>Third key: {object.thirdKey}</p> <button onClick={withPrevState}> Update with prevState </button> <button onClick={() => {setObject({secondKey: 'not empty'})}}> Update without prevState </button> <button onClick={() => {setObject(initialState)}}> Reset </button> </div> ); }; // Render it ReactDOM.render( <Example />, document.getElementById("react") );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="react"></div>
In order to use Maps, you'll need to clone it before manipulating the values. Otherwise, it's mutating the original Map
and React doesn't handle mutatable state
.
const handleChange = useCallback(({ target: { name, checked } }) => { setCheckbox(prevState => { return new Map(prevState).set(name, checked); }); }, []);
Updated Working Example:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With