I'm using React context for state management and code climate doesn't like the repetition around Context Providers.
Is there any way that I can remove the duplication?
I'm using React context for state management and code climate doesn't like the repetition around Context Providers.
Is there any way that I can remove the duplication?
The Code climate rule can be relaxed for this particular code.
That said, you can transform the current usage of context to a React component exposing its props as a channel to provide values in a generic context provider.
For example, a WithCollection component which forwards its props to a generic CollectionContext.Provider can be created. The consolidates the function TasksContext.Provider and CommentsContext.Provider in CollectionContext.Provider.
class WithCollection extends React.Component {
render() {
const {children, ...value} = this.props
return (
<CollectionContext.Provider value={value}>
{children}
</CollectionContext.Provider>
);
}
}
The usage of which is represented as follow:
<WithCollection tasks={this.state.tasks}>
//...
</WithCollection>