1 Star 0 Fork 0

学习笔记区/taro-redux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Redux 的使用笔记

date: 2021-11-18 23:00:00

auther: weisen

文档: https://www.redux.org.cn/


创建 store

src/store/index.ts

import { createStore } from 'redux';
import reducer from '../reducers';

const store = createStore(
  reducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() // Redux DevTools 配置 (chrome 扩展)
);

export default store;

创建 reducers

src/reducers/index.ts

const defaultState = {
  inputValue: 'Write Something',
  list: [
    '早8点开晨会,分配今天的代码任务',
    '早9点和项目经理开需求沟通会',
    '晚5点组织人员进行Review代码',
  ],
};

export default (state = defaultState, action) => {
  return state;
}

使用

src/pages/users/index.tsx

import store from '../../store';

...
constructor(props) {
    super(props);
    this.state = store.getState();
}
...

网络

Taro.request(option)

  • 示例
// Taro.request({
//     url: 'http://public-api-v1.aspirantzhang.com/users',
//     data: {},
//     header: { 'content-type': 'application/json' }, // 默认值
//     success: function (res) {
//         console.log(res.data)
//     }
// });

const result = await Taro.request({
    url: 'http://public-api-v1.aspirantzhang.com/users',
    method: 'GET',
    data: {},
    header: { 'content-type': 'application/json' }, // 默认值
});
console.log({ result });

中间件(redux-thunk)

src/store/index.ts

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import reducer from '../reducers';


// const store = createStore(
//   reducer,
//   window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
// );

const composeEnhancers = typeof window === 'object' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
  }) : compose;

const enhancer = composeEnhancers(applyMiddleware(thunk))

const store = createStore(reducer, enhancer);

export default store;

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/study_note_area/taro-redux.git
git@gitee.com:study_note_area/taro-redux.git
study_note_area
taro-redux
taro-redux
master

搜索帮助