简体   繁体   English

如何访问react Context.Provider值内的另一个函数?

[英]How can I access another function inside the react Context.Provider value?

At the moment, I learn how to use react context API. 目前,我学习了如何使用react context API。 I have a react Provider class, with some state data and functions in the value={} . 我有一个react Provider类,在value={}有一些状态数据和函数。 How can I access a function inside this value from another function inside this value? 如何从该值内的另一个函数访问该值内的函数?

So, the function1() is called by a child component. 因此, function1()由子组件调用。 When the state change is finished, I want to call the function2() and access the new state. 状态更改完成后,我想调用function2()并访问新状态。

Is it possible in react to get something like this?: 是否有可能做出这样的反应?:

class Provider extends Component {
  state = {
    foo: 'bar'
  }

  render() {
    return() {
      <Context.Provider value={{
        state: this.state,

        function1: () => {
          this.setState({ foo: 'changed' }, () => {
            // HERE I WANT TO CALL THE NEXT FUNCTION IN THIS VALUE
            this.function2()
          });
        },

        function2: () => {
          // called by function1 after state change
          console.log(this.state)
        }
      }}>
        { this.props.children }
      </Context.Provider>
    }
  }
}

If I try to run this, and fire the function1() from the child, it gives me 如果我尝试运行此代码,并从孩子那里触发function1() ,它将给我

TypeError: _this2.function2() is not a function

I don't understand, why it is trying to access a _this2 variable, because I defined it to access this.function2() . 我不明白,为什么它要尝试访问_this2变量,因为我定义了它来访问this.function2()

Is it just not possible to do what I want in react? 只是不可能做我想做的事情吗? You might say, why the function2() is an extra function, and why I don't add the code in the end of the function1() . 您可能会说,为什么function2()是一个额外的函数,为什么我不在function1()的末尾添加代码。 It's because the function2() has to be a independent accessable function. 这是因为function2()必须是一个独立的可访问函数。

Can anyone help me? 谁能帮我? Thanks! 谢谢!

What you can try to do is this:- 您可以尝试做的是:

class Provider extends Component {
    state = {
    foo: 'bar'
  }

  an_independent_function() {
    console.log(this.state);
  }

  render() {
    return() {
      <Context.Provider value={{
        state: this.state,

        function1: () => {
          this.setState({ foo: 'changed' }, () => {
            // HERE I WANT TO CALL THE NEXT FUNCTION IN THIS VALUE
            this.an_independent_function();
          });
        },

        function2: this.an_independent_function.bind(this)
      }}>
        { this.props.children }
      </Context.Provider>
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native Context - 从渲染之外的 Context.provider 中检索值 function - React Native Context - retrieve the value from Context.provider outside of the render function React context.provider不会更改默认值 - React context.provider doesn't change the default value 在 React 中将多个值和设置器对传递给 Context.Provider - Passing multiple value and setter pairs to Context.Provider in React 如何在 react 和 typescript 中使用 context.provider 设置 state? - How to set the state using context.provider in react and typescript? 为什么 React Context.Provider 是必需的(或有用的)? - Why Is React Context.Provider Necessary (Or useful)? React - value 属性是必需的<Context.Provider> . 你是拼错了还是忘记通过了? - React - The value prop is required for the <Context.Provider>. Did you misspell it or forget to pass it? 我正在尝试使用挂钩来管理Context.Provider的状态 - I am attempting to use hooks to manage the state of Context.Provider 如何创建类似“ context.Provider” /“ context.Consumer”的结构以在机器人应用中传递值? - How to create a `context.Provider`/`context.Consumer`-like structure to pass values in a bot app? 如何在配置函数中访问服务或访问服务中的提供程序 - How can I access a service in config function or access a provider inside a service 如何访问另一个函数 jquery 中的变量 - How can I access variable inside another function jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM