-
Notifications
You must be signed in to change notification settings - Fork 3
/
React-Redux-Lifecyle-Depth.js
132 lines (126 loc) · 3.46 KB
/
React-Redux-Lifecyle-Depth.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//https://codesandbox.io/s/l531n0n439
import React from "react";
import PropTypes from "prop-types";
import { render } from "react-dom";
const reducers = (state = {}, action) => {
switch (action.type) {
case "initiate":
return Object.assign({ number: 0 }, state);
case "add":
return Object.assign(state, { number: state.number + 1 });
case "sub":
return Object.assign(state, { number: state.number - 1 });
case "save":
return Object.assign(state, { number: action.number });
}
};
const createStore = (initialState = {}, reducers) => {
let state = initialState;
let listeners = [];
const getState = () => state;
const dispatch = action => {
state = reducers(state, action);
listeners.forEach(listener => listener());
};
const subscribe = listener => {
listeners.push(listener);
return () => {
listeners = listeners.filter(l => l !== listener);
};
};
return { getState, dispatch, subscribe };
};
var store = createStore({}, reducers);
const unsubscribe = store.subscribe(() => {
console.log(JSON.stringify(store.getState()));
});
store.dispatch({ type: "initiate" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "sub" });
store.dispatch({ type: "sub" });
//unsubscribe();
store.dispatch({ type: "sub" });
class App extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
parseInt(e.target.value)
? this.props.dispatch({ type: "save", number: parseInt(e.target.value) })
: "";
this.forceUpdate();
}
componentDidMount() {
console.log("componentDidMount");
}
componentWillUnmount() {
console.log("componentWillUnmount");
}
componentDidUpdate(prevProps, prevState, snapshot) {
console.log("componentDidUpdate");
console.log(prevProps, prevState, snapshot);
}
getDerivedStateFromProps(props, state) {
console.log("getDerivedStateFromProps");
}
getSnapshotBeforeUpdate(prevProps, prevState) {
console.log("getSnapshotBeforeUpdate");
}
shouldComponentUpdate(nextProps, nextState) {
console.log("shouldComponentUpdate");
console.log(nextProps, nextState);
return true;
}
componentDidCatch(error, info) {
console.log("componentDidCatch");
console.log(error, info);
}
UNSAFE_componentWillMount() {
console.log("UNSAFE_componentWillMount");
}
UNSAFE_componentWillReceiveProps(nextProps) {
console.log("UNSAFE_componentWillReceiveProps");
}
UNSAFE_componentWillUpdate(nextProps, nextState) {
console.log("UNSAFE_componentWillUpdate");
}
render() {
return (
<div>
<input
type="button"
value="+"
onClick={() => this.props.dispatch({ type: "add" })}
/>
<input
type="text"
value={this.props.number}
onChange={this.handleChange}
/>
<input
type="button"
value="-"
onClick={() => this.props.dispatch({ type: "sub" })}
/>
</div>
);
}
}
App.propTypes = {
number: PropTypes.number,
dispatch: PropTypes.func
};
store.subscribe(() =>
render(
<App number={store.getState().number} dispatch={store.dispatch} />,
document.getElementById("app")
)
);
store.dispatch({ type: "sub" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });