第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 props 從函數(shù)傳遞給全局上下文提供者

將 props 從函數(shù)傳遞給全局上下文提供者

元芳怎么了 2021-08-26 17:11:52
我尋求幫助,了解如何傳遞props到上下文提供從內(nèi)部的function一個的child組成部分。我有一個全局提供程序,我想data從表單中保存它,更具體地說error是onSubmit失敗時的數(shù)據(jù)。使用createContext我有一個全球供應(yīng)商import React, { createContext } from 'react';interface InterfaceProps {  children?: any;}interface InterfaceState {  error: any;  toggleAuthError: any;}const GlobalContext = createContext({  error: null,  toggleAuthError: () => {}});export class GlobalProvider extends React.Component<InterfaceProps, InterfaceState> {  public toggleAuthError = ({ authError }: any) => {    this.setState({ error: authError });  };  public state = {    error: null,    toggleAuthError: this.toggleAuthError  };  public render() {    const { children } = this.props;    return <GlobalContext.Provider value={this.state as any}>{children}</GlobalContext.Provider>;  }}export const GlobalConsumer = GlobalContext.Consumer;然后在我的表單child組件中,我有一個名為onSubmit.public handleFormSubmit = async (data: { email: string; password: string }) => {    const { form } = this.props;    const { email, password } = data;    await form      ...      .catch((error: any) => {        toggleAuthError(error); // Want this to trigger the function and pass error to the context provider        this.setState({          loading: false        });      });  };如何在上下文提供程序中傳遞error給this.toggleAuthError?
查看完整描述

2 回答

?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊

您應(yīng)該使用 GlobalConsumer 包裝您的子組件,以便訪問上下文值。像這樣的東西:


<MyContext.Consumer>

  {({toggleAuthError}) => /* render something based on the context value */}

</MyContext.Consumer>

然后您可以簡單地將 傳遞toggleAuthError給 handleFormSubmit 函數(shù)。


查看完整回答
反對 回復(fù) 2021-08-26
  • 2 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號