J
J
js_web2018-10-02 13:52:10
React
js_web, 2018-10-02 13:52:10

How to forward the data inside the context?

Tell me pliz, I call the this.props.children function, react components are transferred to it (I don’t need them), how to make sure that the data is transferred there, i.e. context.data

<Provider value={{ data: [1, 2, 3333] }}>
          <User>
            {data => {
              return <div>{data}</div>;
            }}
          </User>
        </Provider>

class Header extends Component {
  render() {
    return (
        <div>{this.props.children(<Consumer>{context => context.data}</Consumer>)}</div>
    );
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2018-10-02
@js_web

The meaning of the context is that the data is available at any level of nesting, so there is no need to pass the data explicitly.
I use HOC to conveniently pass data from context to components: https://jsfiddle.net/n5u2wwjg/206004/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question