Skip to content

Commit

Permalink
[context] prevent legacy warning
Browse files Browse the repository at this point in the history
This PR ensures that warnings about legacy context are now shown
when the new context api is used.
  • Loading branch information
JorgenEvens committed May 10, 2020
1 parent 55742e7 commit bd2a421
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions src/context.js
Expand Up @@ -21,32 +21,10 @@ const contextKeys = Object.keys(defaults);
* fall back to the legacy context api.
*/


const ConfigContext = React.createContext && React.createContext();
const ConfigConsumer = ConfigContext ? ConfigContext.Consumer : null;

export const withConfig = (Component) => {
function withAvatarConfig(props, context = {}) {
const { reactAvatar } = context;

if (!ConfigConsumer)
return ( <Component {...defaults} {...reactAvatar} {...props} /> );

/* eslint-disable react/display-name */
return (
<ConfigConsumer>
{config => ( <Component {...defaults} {...config} {...props} /> )}
</ConfigConsumer>
);
/* eslint-enable react/display-name */
}
const isLegacyContext = !ConfigContext;

withAvatarConfig.contextTypes = {
reactAvatar: PropTypes.object
};

return withAvatarConfig;
};
const ConfigConsumer = isLegacyContext ? null : ConfigContext.Consumer;

export class ConfigProvider extends React.Component {

Expand All @@ -61,16 +39,6 @@ export class ConfigProvider extends React.Component {
children: PropTypes.node
}

static childContextTypes = {
reactAvatar: PropTypes.object
}

getChildContext() {
return {
reactAvatar: this._getContext()
};
}

_getContext() {
const context = {};

Expand All @@ -85,7 +53,7 @@ export class ConfigProvider extends React.Component {
render() {
const { children } = this.props;

if (!ConfigContext)
if (isLegacyContext)
return React.Children.only(children);

return (
Expand All @@ -96,3 +64,31 @@ export class ConfigProvider extends React.Component {
}

}

export const withConfig = (Component) => {
function withAvatarConfig(props, context = {}) {

if (!ConfigConsumer)
return ( <Component {...defaults} {...context.reactAvatar} {...props} /> );

/* eslint-disable react/display-name */
return (
<ConfigConsumer>
{config => ( <Component {...defaults} {...config} {...props} /> )}
</ConfigConsumer>
);
/* eslint-enable react/display-name */
}

// Legacy support, only set when legacy is detected
withAvatarConfig.contextTypes = ConfigProvider.childContextTypes;

return withAvatarConfig;
};

if (isLegacyContext) {
ConfigProvider.childContextTypes = { reactAvatar: PropTypes.object };
ConfigProvider.prototype.getChildContext = function() {
return { reactAvatar: this._getContext() };
};
}

0 comments on commit bd2a421

Please sign in to comment.