-
Notifications
You must be signed in to change notification settings - Fork 46
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
**Feature:** OperationalContext hook useOperationalContext #911
Conversation
@adeelibr 💯% yes |
@adeelibr let's add an example to the README so we see it in the playground. 😉 |
Can you kindly review again. @TejasQ |
I made one small change in + const OperationalContext: React.SFC<Props> = props => {
+ const ctx = useOperationalContext()
+ return <React.Fragment>{props.children({ ...ctx })}</React.Fragment>
+ }
- const OperationalContext: React.SFC<Props> = props => (
- <OperationalContextOriginal>{props.children}</OperationalContextOriginal>
- ); This was based on https://americanexpress.io/hydra/ Which uses context under the hood, to support render-props. This way we don't have to manage both of them at the same time. It is working fine at my end, on the local dev-server. I don't know why the deployment is failing here. I tried running this in the <OperationalContext>
{operationalContext => (
<p>{`The viewport is ${operationalContext.windowSize.width} pixels wide and ${
operationalContext.windowSize.height
} tall.`}</p>
)}
</OperationalContext> |
Never mind, I forgot to export |
@adeelibr you got it. @fabien0102 let's review this. 🚀 |
@adeelibr @fabien0102 one final thing that concerns me, is that if this component is used in a class component, the hook will throw for no fault of the user and be a very surprising breakage iiuc. Can we protect against this somehow? |
@TejasQ Can you add an example for context? I don't really when what throw 😄 |
The new linters rule will specify that hooks should not be used in class components. I don't see a work around for this I am afraid. |
here's an example // Legacy code
class MySomething extends React.Component {
render() {
return (<OperationalContext>
{({ pushState }) => <a onClick={() => pushState('/my-chicken-loves-me')}>HELLO 🐔❤️</a>}
</OperationalContext>)
}
} The new
and the whole app will revert to an error boundary or unmount. The user will be like 🤔 wat |
Works fine at my end, doesn't throw an error & it won't throw an error. Because we are using that hook inside our render prop component. The way people access it using Reference: https://americanexpress.io/hydra/ So the new changes are not breaking changes, this PR ensures that the current implementation doesn't break while ensuring that the new implementation also works. This will make it easy to transition slowly in your entire application to use context the new way. (If you decide to refactor) |
@adeelibr I love you let's add more hooks WOOOOOO |
Summary
Keeping the current
render props
implementation in tact, add another function calleduseOperationalContext
which returns the currentcontext
ofOperationalContext
.Uses Hydra pattern https://americanexpress.io/hydra/
usage
P.S: I have not commited
package.json
with the updatedreact
&react-dom
version, should I for this PR?CC: @fabien0102 @TejasQ
Related issue
#908
#910
To be tested
Me
localhost:6060