admin管理员组文章数量:1318032
I am using getInitialProps()
in _App.js
to grab some api data and I want to pass that data down as props to my other ponents via React Context. I want to initialize the Context Provider with state via the constructor.
First I initialize context via createContext()
.
config/Context.js
:
import { createContext } from 'react';
const Context = createContext();
export default Context;
I am using getInitialProps()
in _App.js
to grab some api data and I want to pass that data down as props to my other ponents via React Context. I want to initialize the Context Provider with state via the constructor.
First I initialize context via createContext()
.
config/Context.js
:
import { createContext } from 'react';
const Context = createContext();
export default Context;
Then I create Context.Provider in its own ponent using constructor to initialize state.
provider/ContextProvider.js
:
import React, { Component } from 'react';
import Context from '../config/Context';
class ContextProvider extends Component {
constructor(props) {
super(props);
this.state = {
filters: {
active: true,
headerActive: false
}
};
}
render() {
return (
<Context.Provider>
{this.props.children}
</Context.Provider>
);
}
}
export default ContextProvider;
In _app.js
I make an API call within getInitialProps()
and pass that data into the Context Provider.
pages/_app.js
import App, { Container } from 'next/app';
import ContextProvider from '../provider/ContextProvider';
import fetch from 'isomorphic-unfetch';
export default class MyApp extends App {
static async getInitialProps() {
const res = await fetch('https://node-hnapi.herokuapp./news');
let data = await res.json();
console.log(data)
return { articles: data }
}
render () {
const { Component, pageProps } = this.props;
return (
<Container>
<ContextProvider value={{ articles: this.props.articles}}>
<Component {...pageProps} />
</ContextProvider>
</Container>
);
}
}
At this point I assume that I would have access to context via Context.Consumer
or a hook like useContext()
but context is undefined in the following ponent:
ponents/ArticleList.js
:
import React from 'react';
import Context from '../config/Context';
const ArticleList = () => {
const generateArticles = () => {
const context = React.useContext(Context);
console.log(context, 'context') // Context is undefined here
// return context.articles.map(article => <p>{article.title}</p>)
// Error: Cannot read property 'articles' because context is undefined
}
return (
<div>
<h3>Article List</h3>
{generateArticles()}
</div>
);
};
export default ArticleList;
Why is context undefined in ponents/ArticleList.js
? I tried passing context into the ponent via Context.Consumer
and I got the same result.
Here is repo replicating the issue: https://github./joelhoelting/next-context-api-test
Share Improve this question edited Mar 21, 2019 at 22:14 Joel Hoelting asked Mar 21, 2019 at 22:03 Joel HoeltingJoel Hoelting 1,9923 gold badges26 silver badges45 bronze badges1 Answer
Reset to default 7In ContextProvider.js
you forget to pass value
to Context.Provider
import React, { Component } from 'react';
import Context from '../config/Context';
class ContextProvider extends Component {
constructor(props) {
super(props);
this.state = {
filters: {
active: true,
headerActive: false
}
};
}
render() {
const { value } = this.props
return (
<Context.Provider value={ value }>
{this.props.children}
</Context.Provider>
);
}
}
export default ContextProvider;
本文标签:
版权声明:本文标题:javascript - Next.JS -- getInitialProps to pass props and state to child components via ContextProvider - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742024925a2415361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论