Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React上下文API中调用与调用函数并行的函数_Javascript_Reactjs_Jsx - Fatal编程技术网

Javascript 如何在React上下文API中调用与调用函数并行的函数

Javascript 如何在React上下文API中调用与调用函数并行的函数,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,我在WebContextProvider中有一个函数update(),我想从中调用另一个函数updateAgain(),该函数也出现在WebContextProvider中。以下是参考代码 import React, { createContext, Component } from 'react'; export const WebContext = createContext(); class WebContextProvider extends Component { stat

我在
WebContextProvider
中有一个函数
update()
,我想从中调用另一个函数
updateAgain()
,该函数也出现在
WebContextProvider
中。以下是参考代码

import React, { createContext, Component } from 'react';

export const WebContext = createContext();

class WebContextProvider extends Component {
    state = {
        someState: 1,
    };

    render() {
        return (
            <WebContext.Provider
                value={{
                    data: ...this.state,
                    update: () => {
                       //call updateAgain() from here
                    },
                    updateAgain:() => {
                       //call this from update()
                    }
               }}
            >
                {this.props.children}
            </WebContext.Provider>
        );
    }
}

export default WebContextProvider;
import React,{createContext,Component}来自“React”;
export const WebContext=createContext();
类WebContextProvider扩展组件{
状态={
某个州:1,
};
render(){
返回(
{
//从这里调用updateAgain()
},
UpdateGain:()=>{
//从update()调用此函数
}
}}
>
{this.props.children}
);
}
}
导出默认WebContextProvider;

您可以使用
this.propertyName
引用对象实例上的任何属性,前提是您使用常规
函数而不是箭头函数

const ctxObject={
第一:()=>{
控制台日志(“第一”);
},
第二:函数(){
控制台日志(“第二”);
这个;
}
}

ctxObject.second()可以在类声明上方声明函数,并在上下文提供程序的值中提供它们,或者如果需要访问状态,则必须在类中定义它们并发送对方法的引用

外部功能示例:

import React, { createContext, Component } from 'react';

export const WebContext = createContext();

const update = () => { /* Do something, you can call here updateAgain() */ };

const updateAgain = () => { /* Do something else */ };

export default class WebContextProvider extends Component {
    state = {
        someState: 1,
    };

    render() {
        return (
            <WebContext.Provider
                value={{
                    data: ...this.state,
                    update,
                    updateAgain
                }}>
                {this.props.children}
            </WebContext.Provider>
        );
    }
}
import React, { createContext, Component } from 'react';

export const WebContext = createContext();

export default class WebContextProvider extends Component {
    state = {
        someState: 1,
    };

    render() {
        return (
            <WebContext.Provider
                value={{
                    data: ...this.state,
                    update: this.update,
                    updateAgain: this.updateAgain
                }}>
                {this.props.children}
            </WebContext.Provider>
        );
    }

    update = () => { /* Do something, you can call here this.updateAgain() or use this.state */ }

    updateAgain = () => { /* Do something else, you can use this.state here */ }
}