forked from ioof-holdings/redux-subspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubspaceProvider.js
51 lines (45 loc) · 1.31 KB
/
SubspaceProvider.js
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
/**
* Copyright 2017, IOOF Holdings Limited.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, { useContext, useMemo } from "react"
import PropTypes from "prop-types"
import { ReactReduxContext, Provider } from "react-redux"
import { useSubspaceAdvanced } from "../hooks/useSubspace"
const SubspaceProvider = ({
mapState,
namespace,
subspaceDecorator,
context = ReactReduxContext,
children
}) => {
const {
parent: ParentContext = context.Consumer ? context : ReactReduxContext,
child: ChildContext = context.Consumer ? context : ReactReduxContext
} = context
const subspacedStore = useSubspaceAdvanced(
mapState,
namespace,
subspaceDecorator,
{ context: ParentContext }
)
return (
<Provider store={subspacedStore} context={ChildContext}>
{children}
</Provider>
)
}
SubspaceProvider.propTypes = {
children: PropTypes.element.isRequired,
mapState: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
namespace: PropTypes.string,
subspaceDecorator: PropTypes.func,
context: PropTypes.oneOfType([
PropTypes.shape({ parent: PropTypes.object, child: PropTypes.object }),
PropTypes.object
])
}
export default SubspaceProvider