reactjs - NextJS + React 上下文 : How to load data?

标签 reactjs next.js react-context

我已经为我的项目研究了 nextjs (7) + React 上下文。我的问题是如何通过 Provider 获取 getInitialProps 中的数据?

最佳答案

创建一个名为 components/CounterProvider.js 的组件

import React, { Component } from 'react'

    /* First we will make a new context */
    const CounterContext = React.createContext()

    /* Then create a provider Component */
    class CounterProvider extends Component {
      state = {
        count: 0
      }

      increase = () => {
        this.setState({
          count: this.state.count + 1
        })
      }

      decrease = () => {
        this.setState({
          count: this.state.count - 1
        })
      }

      render () {
        return (
          <CounterContext.Provider
            value={{
              count: this.state.count,
              increase: this.increase,
              decrease: this.decrease
            }}
          >
            {this.props.children}
          </CounterContext.Provider>
        )
      }
    }

    /* then make a consumer which will surface it */
    const CounterConsumer = CounterContext.Consumer

    export default CounterProvider
    export { CounterConsumer }

然后在 pages/_app.js 中使用此代码来使用提供程序并在所有组件之间共享它:

import App, { Container } from 'next/app'
    /* First we import our provider */
    import CounterProvider from '../components/CounterProvider'

    class MyApp extends App {
      render () {
        const { Component, pageProps } = this.props
        return (
          <Container>
            {/* Then we wrap our components with the provider */}
            <CounterProvider>
              <Component {...pageProps} />
            </CounterProvider>
          </Container>
        )
      }
    }

    export default MyApp

最后在任何组件中使用它,如下所示:pages/index.js

import React, { Component } from 'react'
    /* First we import the consumer */
    import { CounterConsumer } from '../components/CounterProvider'

    export default class index extends Component {
      render () {
        return (
          /* Then we use our context through render props */
          <CounterConsumer>
            {({ count, increase, decrease }) => (
              <div>
                <p>Counter: {count}</p>
                <button onClick={increase}>Increase</button>
                <button onClick={decrease}>Decrease</button>
              </div>
            )}
          </CounterConsumer>
        )
      }
    }

更多信息请关注this example

关于reactjs - NextJS + React 上下文 : How to load data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54192513/

相关文章:

reactjs - 如何测试componentDidUpdate()?

javascript - 我该如何做顶级等待

typescript - 下一个 js 中的 Rust wasm

javascript - 在 Context 消费者上使用 useContext() 时,React Context "Property ' 状态'不存在

node.js - 在服务器上处理 react 路由的正确方法?

javascript - 点击时 react 获取 id 值

javascript - 下一个js : useEffect only once (if I use Link component)

javascript - 与嵌套组件共享上下文,未定义获取 UserContext

javascript - 在 React Context 中更新嵌套状态的最佳方法

reactjs - 在 slatejs React 中的编辑器内容末尾添加自定义 block