I noticed new feature portals
do the same thing but better? I dont know much about portals but it seems to be the new way to manage nested component updates?
I knew Reacts Context API was experimental, and noticed componentDidUpdate
no longer receives prevContext
and they dropped contextTypes
.
I also noticed they are introducing React 16's Portal API and not sure if this is intended to replace the Context API.
So again, as mentioned above, is React 16's Portal API meant to replace the Context API?
EDIT: To piggyback on this topic, is conext the best way to manage i18n localization in react?
The legacy context API will be removed in a future major version. Use the new context API introduced with version 16.3. The legacy API will continue working for all 16.
If you're only using Redux to avoid passing down props, you can replace it with Context API. Context is great for sharing trivial pieces of state between components. Redux is much more powerful and provides a set of handy features that Context doesn't have.
The React Context API is a way for a React app to effectively produce global variables that can be passed around. This is the alternative to "prop drilling" or moving props from grandparent to child to parent, and so on. Context is also touted as an easier, lighter approach to state management using Redux.
Though context API solves problem of drilling components to pass data around, it introduces complexity in myContext. Consumer because a function has to be passed as its child. If you want to avoid this complexity, you can use useContext .
The previous version of Context Api was experimental whereas the new version in React 16+ is more stable and efficient. Context API is used to pass global variables anywhere in the code. It helps when there is a need for sharing state between a lot of nested components.
Legacy API; When to Use Context . Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language. For example, in the code below we manually thread through a “theme” prop in order to style the Button component:
If using the useContext hook, this will have the new value of the hook during the new render. To understand how React’s Context feature can help us, let’s look at a scenario where the React Context API would make things simpler for us.
const MyContext = React.createContext(defaultValue); Creates a Context object. When React renders a component that subscribes to this Context object it will read the current context value from the closest matching Provider above it in the tree.
TL;DR => Portals
and Context
solve different purposes, one is to inject DOM at any level and other is to inject props at any level. Context
can mimic Portals
but Portals
AS OF NOW cannot mimic Context
not at least without introducing code smell.
NOTE: Below is based on my understanding of the two concepts, if anyone has further thoughts or corrections on this please feel free to edit the answer.
From what I was able to understand Portals
are, as the name suggests, a door way for you to render components that need not be in the component tree hierarchy. This works like perfectly for Modals
, Popovers
or any component which needs to be stitched at a particular location in the tree.
Context
is to communicate with various sibling and child components without having to pass props all the way down from parent to the intended component. Of course, this is an important feature to have but it remained in the experimental phase probably due to the fact that this can be achieved by event-emitters
and Redux
, MobX
for a centralized state management.
I am assuming that your use case for i18n
would require a lot of communication across components for which you might want to look at this great article
COMPONENT COMMUNICATION
Portals and Context help achieving this kind of communication but there is a difference. Portals can render or inject DOM at any level and Context can inject props at any level in the subcomponent tree.
You can always achieve what Portals
are able to do using Context
but I don't think Portals
can mimic Context
functionality.
EG: Something like this can be done to mimic what Portals
do using Context
. Where as in Portals
AFAIK you can only send DOM Nodes ReactDOM.createPortal(child, container)
. May be there is a way to achieve the functionality but that would surely lead to code smell.
class Parent extends React.Component {
getChildContext() {
return {
renderModal: this.renderModal
}
}
renderModal = (children) => {
this.setState({
open: !this.state.open,
injectableChildren: children
})
}
render() {
this.state.open
?
<div>
{this.state.injectableChildren}
</div>
:
null
// JSX
}
}
class SomeSibling extends React.Component {
static contextTypes = {
renderModal: React.PropTypes.func
}
handleOnClick = (event) => {
this.context.renderModal(renderableChildJSX);
}
renderableChildJSX = () => (
<div>
YAY I AM GETTING RENDERED AT THE ROOT
</div>
)
render() {
return(
<div onClick={this.handleOnClick}>
</div>
)
}
}
In my case, I feared from using Context
despite it's flexibility due to the fact that React
docs always mentioned that it's an experimental feature and warned repeatedly from using this feature full-blown. My guess is that React is looking at stabilising this feature or completely scrape it off the React codebase which is a risk that could go both ways.
CONCLUSION: IMO, Portals
in it's current state and the problem it is trying to resolve is entirely different from what Context
is built for and it is better to use event-emitters
with the only reason that Context
might possibly be deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With