|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
## 前期准备
|
|
|
|
|
|
-1.node 环境配置在 14 及以上,可以使用 nvm 进行 node 环境管理
|
|
|
+1.node 环境配置在 12 及以上,可以使用 nvm 进行 node 环境管理
|
|
|
|
|
|
2.下载微信开发者工具
|
|
|
|
|
@@ -125,15 +125,22 @@ react 所有生命周期都适用,项目中最常用的有以下两种:
|
|
|
|
|
|
```js
|
|
|
class App extends Component {
|
|
|
- // 可以使用所有的 React 生命周期方法,一般在此阶段请求接口获取数据
|
|
|
- componentDidMount() {}
|
|
|
-
|
|
|
- // 页面状态或者变量更新时的生命周期
|
|
|
- componentDidUpdate() {}
|
|
|
-
|
|
|
- render() {
|
|
|
- return <div></div>;
|
|
|
- }
|
|
|
+ state = {
|
|
|
+ name: "",
|
|
|
+ };
|
|
|
+ // 可以使用所有的 React 生命周期方法,一般在此阶段请求接口获取数据
|
|
|
+ componentDidMount() {
|
|
|
+ this.setState({
|
|
|
+ name: "xxx",
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 页面状态或者变量更新时的生命周期
|
|
|
+ componentDidUpdate() {}
|
|
|
+
|
|
|
+ render() {
|
|
|
+ return <div></div>;
|
|
|
+ }
|
|
|
}
|
|
|
```
|
|
|
|
|
@@ -153,20 +160,20 @@ class App extends Component {
|
|
|
import { combineReducers } from "redux";
|
|
|
|
|
|
const initState = {
|
|
|
- name: ""
|
|
|
+ name: "",
|
|
|
};
|
|
|
|
|
|
function userInfo(state = initState, action) {
|
|
|
- switch (action.type) {
|
|
|
- case "SAVE":
|
|
|
- return Object.assign({}, state, action.info.user);
|
|
|
- default:
|
|
|
- return state;
|
|
|
- }
|
|
|
+ switch (action.type) {
|
|
|
+ case "SAVE":
|
|
|
+ return Object.assign({}, state, action.info.user);
|
|
|
+ default:
|
|
|
+ return state;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default combineReducers({
|
|
|
- userInfo
|
|
|
+ userInfo,
|
|
|
});
|
|
|
```
|
|
|
|
|
@@ -180,8 +187,8 @@ import Api from "...";
|
|
|
|
|
|
// worker Saga : 将在 INIT action 被 dispatch 时调用
|
|
|
function* fetchUser(action) {
|
|
|
- const user = yield call(Api.fetchUser);
|
|
|
- yield put({ type: "SAVE", info: { user } });
|
|
|
+ const user = yield call(Api.fetchUser);
|
|
|
+ yield put({ type: "SAVE", info: { user } });
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -189,7 +196,7 @@ function* fetchUser(action) {
|
|
|
允许并发(译注:即同时处理多个相同的 action)
|
|
|
*/
|
|
|
function* mySaga() {
|
|
|
- yield takeEvery("INIT", fetchUser);
|
|
|
+ yield takeEvery("INIT", fetchUser);
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -200,7 +207,7 @@ function* mySaga() {
|
|
|
那么处理中的 action 会被取消,只会执行当前的
|
|
|
*/
|
|
|
function* mySaga() {
|
|
|
- yield takeLatest("INIT", fetchUser);
|
|
|
+ yield takeLatest("INIT", fetchUser);
|
|
|
}
|
|
|
|
|
|
export default mySaga;
|
|
@@ -236,55 +243,55 @@ import { withRouter } from "react-router-dom";
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
|
class App extends Component {
|
|
|
- state = {
|
|
|
- nickName: ""
|
|
|
- };
|
|
|
-
|
|
|
- componentDidMount() {
|
|
|
- this.props.onInitUserInfo(); // 调用 props 中的方法获取全局数据
|
|
|
-
|
|
|
- this.getNickName(); // 直接调用接口获取数据
|
|
|
- }
|
|
|
-
|
|
|
- async getNickName() {
|
|
|
- const result = await getNickNameApi();
|
|
|
- this.setState({
|
|
|
- nickName: result.nickName
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- render() {
|
|
|
- const { userInfo } = this.props; // 从 props 中获取数据
|
|
|
- return (
|
|
|
- <div onClick={() => this.props.onChangeUserInfo({ name: "xxx" })}>
|
|
|
- {userInfo.name}
|
|
|
- </div>
|
|
|
- );
|
|
|
- }
|
|
|
+ state = {
|
|
|
+ nickName: "",
|
|
|
+ };
|
|
|
+
|
|
|
+ componentDidMount() {
|
|
|
+ this.props.onInitUserInfo(); // 调用 props 中的方法获取全局数据
|
|
|
+
|
|
|
+ this.getNickName(); // 直接调用接口获取数据
|
|
|
+ }
|
|
|
+
|
|
|
+ async getNickName() {
|
|
|
+ const result = await getNickNameApi();
|
|
|
+ this.setState({
|
|
|
+ nickName: result.nickName,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ const { userInfo } = this.props; // 从 props 中获取数据
|
|
|
+ return (
|
|
|
+ <div onClick={() => this.props.onChangeUserInfo({ name: "xxx" })}>
|
|
|
+ {userInfo.name}
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-const mapStateToProps = state => {
|
|
|
- // 设置所要监听/获取的数据
|
|
|
- return {
|
|
|
- userInfo: state.userInfo
|
|
|
- };
|
|
|
+const mapStateToProps = (state) => {
|
|
|
+ // 设置所要监听/获取的数据
|
|
|
+ return {
|
|
|
+ userInfo: state.userInfo,
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
-const mapDispatchToProps = dispatch => {
|
|
|
- // 设置函数,触发 dispatch,获取/修改用户数据
|
|
|
- return {
|
|
|
- onInitUserInfo() {
|
|
|
- // 初始化数据
|
|
|
- dispatch({ type: "INIT" });
|
|
|
- },
|
|
|
- onChangeUserInfo(user) {
|
|
|
- // 修改数据
|
|
|
- dispatch({
|
|
|
- type: "SAVE",
|
|
|
- info: user
|
|
|
- });
|
|
|
- }
|
|
|
- };
|
|
|
+const mapDispatchToProps = (dispatch) => {
|
|
|
+ // 设置函数,触发 dispatch,获取/修改用户数据
|
|
|
+ return {
|
|
|
+ onInitUserInfo() {
|
|
|
+ // 初始化数据
|
|
|
+ dispatch({ type: "INIT" });
|
|
|
+ },
|
|
|
+ onChangeUserInfo(user) {
|
|
|
+ // 修改数据
|
|
|
+ dispatch({
|
|
|
+ type: "SAVE",
|
|
|
+ info: user,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
// 通过 redux 提供的 connect 方法,给 APP 组件的 props 中连接所需要的属性和方法
|