Question about context
See original GitHub issueGood day to you all. Im not sure this is actually an issue and my question is rather about best practices on your implementation of the context API.
I’m in a position where I’ve implemented it in a way that practically gives me access to a global context in all children’s lifecycles and rendering methods with either this.context
or just the parameter ‘context’ in a stateless component. Since I haven’t seen this implementation anywhere else, I’m worried I might be adapting to a method that is some kind of anti-pattern or has risk of being dangerous. Being pretty green as I am, I thought I would just ask here for a more experienced perspective.
Basically, is there something really bad with this?
Thanks in advance, and please tell me if I’m posting this in the wrong place.
If you take a look at the following:
import { Component } from 'preact'
export default class BookmarkContext extends Component {
state = {
scrollTo: false,
shouldHideOnScroll: true,
scrollOffset: 0
}
getChildContext() {
return {
state: this.state,
setArticleScrollPos: this.setArticleScrollPos,
resetScrollPos: this.resetScrollPos,
setShouldHideOnScroll: this.setShouldHideOnScroll
}
}
setShouldHideOnScroll = (scrollOffset, shouldHide) => this.setState({shouldHideOnScroll: shouldHide, scrollOffset: scrollOffset})
setArticleScrollPos = data => {
this.setState({scrollTo: data})
}
resetScrollPos = () => this.setState({scrollTo: false})
render({ children }) {
return <div>{children}</div>;
}
}
And then I wrap my whole application in this component in app.js:
render() {
return (
<MainContext>
<ThemeProvider theme={ MainTheme }>
<div>
<Header init={this.state.init}/>
<div id="app">
<Router>
<ArticlePreview initAudio={this.handleAudioInit} path="/"/>
<ArticleBase path="article/:id"/>
<ArticleBase path="article"/>
</Router>
</div>
</div>
</ThemeProvider>
</MainContext>
)
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
This is a valid pattern for some apps. The only issue with that is that context updates may be blocked if there is as component somewhere in the tree which has the
shouldComponentUpdate
lifecycle.This is solved via the new context api in react. We have not added it into core yet, but you can use the third party
preact-context
package instead. Another alternative are all the state management solutions out there like redux, mobx, unistore, unstated,…so, We have SSR enabled app.
Context from react using a Reducer is exported from one TS file Imported in Index.tsx (SPA), gets FirstName property updated based on Rest API calls like based on some if-else. API can be different. Once we get Firstname, using React.context function, we update context value ( this is not happening as is ).
To resolve this issue, in Index.tsx page we added a state property for now and context seems to be working but not able to update value from a class component (as we have) using something like this.context.firstname = …
On Sun, Feb 13, 2022 at 10:29 AM Kumar Gaurav @.***> wrote:
– Regards, Kumar Gaurav +1 469 354 2101