简体   繁体   中英

Using nested hooks to update state

I'm trying to figure out how I can use nested hooks to update state in my parent component. I recently learned that hooks basically follow the same setState() async updating pattern but I'm struggling to find my war around that.

I have 3 custom hooks:
useValidateContent : A custom hook to validate a specific form.
useValidateRuleOne : A custom hook to validate a specific rule.
useValidateRuleTwo : A custom hook to validate another specific rule.

I want to use useValidateRuleOne and useValidateRuleTwo in useValidateContent and update the ParentComp on click. Right now it take 2 click for the messages to appear.

What is the proper way to use nested custom hooks so that state updates on first on first run.

 const { useState } = React; const useValidateRuleOne = initState => { const[valid, setValid] = useState(initState) const[errorMsg, setErrorMsg] = useState(null) const validate = () => { // For test purposes just set to false setValid(false) setErrorMsg('There was an error validating rule one') } return [valid, validate, errorMsg] } const useValidateRuleTwo = initState => { const[valid, setValid] = useState(initState) const[errorMsg, setErrorMsg] = useState(null) const validate = () => { // For test purposes just set to false setValid(false) setErrorMsg('There was an error validating rule two') } return [valid, validate, errorMsg] } const useValidateContent = initState => { const [valid, setValid] = useState(initState); const [validOne, validateOne, errorOne ] = useValidateRuleOne(true) const [validTwo, validateTwo, errorTwo ] = useValidateRuleTwo(true) const [errorMsg, setErrorMsg] = useState(""); const validate = () => { validateOne() validateTwo() let errorMsg = ''; if (!validOne) errorMsg += errorOne; if (!validTwo) errorMsg += errorTwo; setErrorMsg(errorMsg) if (errorMsg.length > 1) setValid(false) } return [valid, validate, errorMsg]; }; function ParentComp() { const [contentIsValid, validate, contentError] = useValidateContent(true); const initValidate = () => { // values before running validate validate(); }; return ( <div> <button onClick={initValidate}>initValidate</button> contentIsValid: {contentIsValid.toString()}, contentError: {contentError} </div> ); } ReactDOM.render(<ParentComp />, document.getElementById('root')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/react@16/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root"></div> 

According to Hooks API, you shouldn't call a hook conditionally or inside nested functions. https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

One solution for your issue is to add useEffect, which will fire any time your specified dependencies change. Here's a working example: https://codesandbox.io/s/5zyljjm7o4

Make pure functions from validation rules and store state only on one place. It should be cleaner and also better for testing. https://codesandbox.io/s/r4v026lxno

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM