2

I've researched nextjs (7) + react context for my project. My problem is how to fetch data in getInitialProps by Provider?

1
  • 1
    can you please show some code with what you have tried and what didn't work? Commented Jan 15, 2019 at 4:06

1 Answer 1

2

Create a component called 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 }

then in pages/_app.js use this code to use the provider and share it between all components:

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

finally use it in any component like this: 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>
        )
      }
    }

For more info follow this example

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't answer the question, which is specifically about how to fetch data in the provider. The examples given in this answer do not do any data fetching at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.