-
Notifications
You must be signed in to change notification settings - Fork 11
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
refactor(context): Update to latest React Context API #391
Changes from 14 commits
bb75b23
10e6f5f
394f2a7
89bedb8
17d872c
0ca9ebe
2d5f4be
ee1fa3a
75d5fda
074ece6
4623a5b
246da50
20ca746
5b44972
14e1a40
19e1eb4
6e0d937
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,6 @@ | |
] | ||
], | ||
"env": { | ||
"development": { | ||
"plugins": ["flow-react-proptypes"] | ||
}, | ||
"test": { | ||
"plugins": [ | ||
[ | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import type { ConfigContextType } from '../types' | ||
import Context from './context' | ||
|
||
type Props = { children: (context: ConfigContextType) => ?React.Node } | ||
|
||
export default function ConfigConsumer({ children }: Props) { | ||
return <Context.Consumer>{context => children(context)}</Context.Consumer> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import type { ConfigContextType } from '../types' | ||
import Context from './context' | ||
|
||
type ProviderProps = { | ||
...ConfigContextType, | ||
children?: React.Node, | ||
} | ||
|
||
type ProviderState = ConfigContextType | {} | ||
|
||
// This HOC takes a Provider component and wraps it around a Consumer | ||
// component that provides non-defaulted values that have been set from preceding | ||
// Provider components. | ||
class ConfigProvider extends React.Component<ProviderProps, ProviderState> { | ||
static getDerivedStateFromProps(props: ProviderProps, state: ProviderState) { | ||
const { children, ...restProps } = props | ||
|
||
return { | ||
...state, | ||
...restProps, | ||
} | ||
} | ||
|
||
state = {} | ||
|
||
render() { | ||
return ( | ||
<Context.Consumer> | ||
{(context: ConfigContextType) => { | ||
const { children } = this.props | ||
const value = { ...context, ...this.state } | ||
|
||
return <Context.Provider value={value}>{children}</Context.Provider> | ||
}} | ||
</Context.Consumer> | ||
) | ||
} | ||
} | ||
|
||
export default ConfigProvider |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import type { ConfigContextType } from '../types' | ||
import defaults from '../defaults' | ||
|
||
const contextDefaults: ConfigContextType = defaults | ||
|
||
const Context = React.createContext(contextDefaults) | ||
|
||
export default Context |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @flow | ||
|
||
import ConfigProvider from './configProvider' | ||
import ConfigConsumer from './configConsumer' | ||
|
||
export default { | ||
Provider: ConfigProvider, | ||
Consumer: ConfigConsumer, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why export both? We only need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense. I'll make Provider only exportable inside it's own file but not in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I was working on removing this, I noticed (via Storybook) that when using This is only an issue in Storybook in the Example:
This appears to be a known issue via this issue To alleviate the Storybook problem, I can remove all imports from
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add a babel alias to it to make it a bit more convenient to use. If we define one (for stories only) that maps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to ensure the project is still tree-shakeable with babel-plugin-lodash this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
react-dom
still needed here then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I'll add this back.