forked from jeff-burns/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redux-Themed-Components.jsx
39 lines (29 loc) · 1.31 KB
/
Redux-Themed-Components.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
/**
* So if it’s _just_ prop drilling you’re trying to avoid with one or two sets of data,
* then context API might be the easiest way to implement. But if the data is being updated
* from the connected components, then I like redux better (mostly just because I have the most experience.
* You can (and should) definitely pass connect around IMO. But rather than
* importing connect a million places and creating redundant `mapDispatch` and `mapState` functions,
* you can just make container files that are imported into any component that needs them.
* So for example, you have a data type that’s like USER data.
* This file exports an executed function, that returns another function
*/
// in a file called like...userContainer
import { connect } from 'react-redux'
const mapStateToProps = (state) => ({
email: state.email,
favoriteAnimals: state.userFavorites.animals,
gender: state.gender
})
const mapDispatchToProps = (dispatch) => ({
// I like using bindActionCreators to bind all necessary actions/thunks
})
export default connect(mapStateToProps, mapDispatchToProps);
// in a component file
import userContainer from '../../some/path/userContainer';
const userProfile = (props) => {
return (
<h1>Welcome, {props.email}</h1>
);
}
export default userContainer(userProfile);