-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide example using Context API with custom _app.js #4182
Comments
Exactly. I can give this a shot @timneutkens . Although replacing Redux with just context might be hard. I think SSR is really well done with Redux's reducers way. |
There are libraries that are reduxish but even any global state example that uses _app.js, SSR and getInitialProps on various pages would be really appreciated. 🙏 |
This would be blocked by #4194: Context provided in |
The blocker has been fixed in Next.js 7 canary ( |
@timneutkens Thank you for your hard work! Is there any word on the OP's requested example? |
Feel free to create one, should be relatively easy to figure out 👍 |
I have an example if anyone wants to clean it up and turn it into a PR: NoteProvider.js import React, { Component } from 'react';
// first we will make a new context
const NoteContext = React.createContext();
// Then create a provider Component
class NoteProvider extends Component {
state = {
age: 100,
};
render() {
return (
<NoteContext.Provider
value={{
state: this.state,
growAYearOlder: () =>
this.setState({
age: this.state.age + 1,
}),
}}
>
{this.props.children}
</NoteContext.Provider>
);
}
}
// then make a consumer which will surface it
const NoteConsumer = NoteContext.Consumer;
export default NoteProvider;
export { NoteConsumer }; _app.js
Then some page with that component on it: import React, { Component } from 'react';
import { NoteConsumer } from './NoteProvider';
class NotesList extends Component {
render() {
return (
<div>
<NoteConsumer>
{({ state, growAYearOlder }) => (
<p>
hi I'm {state.age}
<button onClick={growAYearOlder}>Grow</button>
</p>
)}
</NoteConsumer>
</div>
);
}
}
export default NotesList; |
@wesbos i just try your code. But whenever it's server side rendered i get "Cannot read property 'state' of undefined" on NotesList. Works fine on client side render. I'm on next 6.1.2 |
You need next@canary - it’s in 7.0
On September 12, 2018 at 6:31 PM, maurodaprotis ([email protected]) wrote:
@wesbos ( https://github.com/wesbos ) i just try your code. But
whenever it's server side rendered i get "Cannot read property
'state' of undefined" on NotesList. Works fine on client side
render.
I'm on next 6.1.2
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
( #4182 (comment) )
, or mute the thread
( https://github.com/notifications/unsubscribe-auth/AAKvjS2J2U6eEKqFmN7gDB4drr6dfFYtks5uaYsvgaJpZM4Tc3o- )
.
|
Here is another example of Next Js and React Context I also added styled components https://github.com/NangoliJude/React-Context-Api-with-Next-Js |
question, how do I access context api in _app.js (getInitialProps) ., this is my use case, get refresh token on page load (if cookie is not present), store the returned session info in context ! this should happen if I hit any of the pages. |
I run into the same issue. You can pass a prop to the context provider but it will not be available until render of the app component. |
@maurodaprotis ,any solutions? |
Cannot find context namespace with typescript |
Does anyone have an example that doesn't use a Consumer, but rather: Having trouble getting it working without a Consumer! |
That'd only work on newer versions of React (16.6), make sure you're on that version |
Just throwing it out there, but the package easy-peasy makes it insanely easy to do this with Next now. It's a wrapper around React Hooks. |
I woudn't use |
Hello, Context file`import React, { createContext, useState } from 'react'; export const AppContext = createContext(); const baseURL = "api/v1/"; const AppContextProvider = (props) => {
} export default AppContext ` _app.js file//import '../styles/bootstrap.min.css'; import Router from 'next/router'; //context not found
} |
@wesbos Any way you can provide the hooks version? I was hoping to connect to a socket server and respond to its callbacks through the page root so that page transitions doesn't loose the connection to the server. I have problems trying to get the
Update I tried this approach of calling |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
It would be great for those of us looking to use React's new Context API and the new custom _app.js in Canary to have an example app to view.
The custom app component persists state between pages and seems like a great way to make use of the Context API at the app container level. Please provide an example showing how to set this up with providers, consumers and multiple pages and how to best deal with server-side rendering and getInitialProps. Basically, how to replace Redux with Context.
The text was updated successfully, but these errors were encountered: