代码之家  ›  专栏  ›  技术社区  ›  Rilcon42

将函数的结果向上传递到链中,这样父级就会发生变化

  •  0
  • Rilcon42  · 技术社区  · 4 年前

    我有一个功能 PostQuestionForm 打电话 signin . 签名 创建表单。如何创建表单 签名 通知 邮寄调查表 发生了变化。

    export function PostQuestionForm() {
        const [isLoading, setIsLoading] = useState(false);
    
        return (
            <div>
                { creds.username == 'xxx' ?
                        (<div>
                            success
                        </div>) :
                        (<SigninForm />)
                }
            </div>
        )
    }
    

    登录

    export function SigninForm() {
    
        //formSubmit calls this function
        var credsSubmitHandler = async (event) => {} //creds variable is updated here
    
    
    return (....form here...)
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   hgb123    4 年前

    你可以为创建一个州 creds

    邮寄调查表

    export function PostQuestionForm() {
        const [isLoading, setIsLoading] = useState(false);
        const [creds, setCreds] = useState({})
    
        return (
            <div>
                { creds.username == 'xxx' ?
                        (<div>
                            success
                        </div>) :
                        (<SigninForm setCreds={setCreds} />)
                }
            </div>
        )
    }
    

    登录

    export function SigninForm({ setCreds }) {
    
        //formSubmit calls this function
        var credsSubmitHandler = async (event) => {
          //... do your business here
          setCreds(result)
        } //creds variable is updated here
    
    
    return (....form here...)
    }