This repository was archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.jsx
60 lines (55 loc) · 1.73 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import createGetProviderConfig from 'selectors/createGetProviderConfig';
import toProvider from 'utils/toProvider';
import toReactElement from 'utils/toReactElement';
const ConnectedProvider = (props) => {
const {
children,
config,
name,
} = props;
return toProvider(name, config, children);
};
ConnectedProvider.propTypes = {
/**
* Provider name.
*/
name: PropTypes.string.isRequired,
/**
* Component configuration (mapped to props)
*/
config: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
/**
* Component children
*/
children: PropTypes.arrayOf(PropTypes.object).isRequired,
};
/**
* Each ConnectedProvider component will receive it's own copy of the state mapper,
* which allows the createGetProviderConfig factory to create a memoized copy
* of the getComponent selector with access to the component's props.
*
* In essence this allows each ConnectedRoot to be isolated, and only re-render
* when its own related Redux state changes. For example, a ConnectedRoot that
* renders the body component will only update if the corresponding Redux body
* component state changes.
* @returns {function} - Redux state mapper function
*/
const createMapStateToProps = () => {
const getProviderConfig = createGetProviderConfig();
return function mapStateToProps(state, props) {
const {
config,
children: apiChildren = [],
} = getProviderConfig(state, props);
return {
config,
children: apiChildren
.map(toReactElement)
.concat(props.children),
};
};
};
const withRedux = connect(createMapStateToProps);
export default withRedux(ConnectedProvider);