Download react-boilerplate/internals/templates/utils/injectReducer.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-boilerplate/internals/templates/utils/injectReducer.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-boilerplate/internals/templates/utils/injectReducer.js
-
curl -L -o injectReducer.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-boilerplate/internals/templates/utils/injectReducer.js
1.18 kB
| import React from 'react'; | |
| import hoistNonReactStatics from 'hoist-non-react-statics'; | |
| import { ReactReduxContext } from 'react-redux'; | |
| import getInjectors from './reducerInjectors'; | |
| /** | |
| * Dynamically injects a reducer | |
| * | |
| * @param {string} key A key of the reducer | |
| * @param {function} reducer A reducer that will be injected | |
| * | |
| */ | |
| export default ({ key, reducer }) => WrappedComponent => { | |
| class ReducerInjector extends React.Component { | |
| static WrappedComponent = WrappedComponent; | |
| static contextType = ReactReduxContext; | |
| static displayName = `withReducer(${WrappedComponent.displayName || | |
| WrappedComponent.name || | |
| 'Component'})`; | |
| constructor(props, context) { | |
| super(props, context); | |
| getInjectors(context.store).injectReducer(key, reducer); | |
| } | |
| render() { | |
| return <WrappedComponent {...this.props} />; | |
| } | |
| } | |
| return hoistNonReactStatics(ReducerInjector, WrappedComponent); | |
| }; | |
| const useInjectReducer = ({ key, reducer }) => { | |
| const context = React.useContext(ReactReduxContext); | |
| React.useEffect(() => { | |
| getInjectors(context.store).injectReducer(key, reducer); | |
| }, []); | |
| }; | |
| export { useInjectReducer }; | |