-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathLayoutContainer.tsx
More file actions
48 lines (39 loc) · 1.7 KB
/
LayoutContainer.tsx
File metadata and controls
48 lines (39 loc) · 1.7 KB
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
import * as React from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { ApplicationState } from '../store'
import { ThemeColors } from '../store/layout'
import * as layoutActions from '../store/layout/actions'
// Now here is an example of creating container components.
//
// Before React v16 I would've suggested against implementing container components that are
// separate from their connected view logic, since they intrude at the very definition of a view,
// but now with newer patterns (e.g. render props), it makes sense to use them once again.
//
// See how this works at `./src/components/Header`
// Redux-specific props.
interface LayoutContainerProps {
theme: ThemeColors
setTheme: (theme: ThemeColors) => void
}
// Wrapper props for render/children callback.
interface LayoutContainerRenderProps {
render?: (props: LayoutContainerProps) => React.ReactElement
children?: (props: LayoutContainerProps) => React.ReactElement
}
const LayoutContainer: React.FC<LayoutContainerRenderProps> = ({ render, children }) => {
// We can use Hooks to call in our selector/dispatch functions.
const { theme } = useSelector((state: ApplicationState) => state.layout)
const dispatch = useDispatch()
// Create the `setTheme` handler. We use the `dispatch` we got from `useDispatch()` to create said selector.
const setTheme = (color: ThemeColors) => dispatch(layoutActions.setTheme(color))
console.log(localStorage.getItem('theme'))
// Create a render/children props wrapper with the above variables set as a callback.
if (render) {
return render({ theme, setTheme })
}
if (children) {
return children({ theme, setTheme })
}
return null
}
export default LayoutContainer