redux与react联合使用
2018-02-10 09:55
519 查看
redux是用来状态管理的,react是view层框架,两者配合来创建一个复杂的单页应用。
1、index.redux.js负责redux部分
2、App.js组件属于react
3、index.js组合react和redux
1、index.redux.js负责redux部分
const ADD_GUN = '加机关枪'; const REMOVE_GUN = '减机关枪'; //建立reducer,根据state和action,返回一个新的state export function counter(state = 0, action) { switch(action.type) { case ADD_GUN: return state + 1; case REMOVE_GUN: return state - 1; default: return state; } } // action creator export function addGUN() { return {type: ADD_GUN}; } export function removeGUN() { return {type: REMOVE_GUN}; }
2、App.js组件属于react
import React from 'react'; import {addGUN, removeGUN} from './index.redux'; class App extends React.Component { render() { const store = this.props.store; const num = store.getState(); return( <div> <h1>现在有{num}把机枪</h1> <button onClick={() => store.dispatch(addGUN())}> 申请武器 </button> <button onClick={() => store.dispatch(removeGUN())}> 撤销武器 </button> </div> ) } } export default App;
3、index.js组合react和redux
import React from 'react'; import ReactDom from 'react-dom'; import {createStore} from 'redux'; import App from './App'; import {counter} from './index.redux'; const store = createStore(counter); function render() { ReactDom.render(<App store={store}/>, document.getElementById('root')); } render(); store.subscribe(render);
相关文章推荐
- [React Native]Redux的基本使用方式
- react-redux中connect装饰器的使用
- react-redux使用小结
- 基于react+redux的轻量级框架dva初使用及应用逻辑分析
- React/Redux应用使用Async/Await
- 如何使用webpack+react+redux从头搭建Todolist应用
- 关于React-Native使用immutable(redux环境下)的一点用法
- React第三方组件5(状态管理之Redux的使用⑤异步操作)
- redux在react-native上使用(三)--加入redux-thunk
- React Native之Redux使用详解之Reducers(30)
- react native+redux 使用jest进行unit test
- redux在react-native中的使用
- [置顶] (十)Redux:官方react-redux的初步使用(Provider组件、connect函数)
- [译文&摘抄]在 React & Redux 中使用 AJAX 轮询
- Redux 17 - 进阶:和React Router一起使用(Usage with React Router)
- react-redux的简单使用
- 为什么 react 中要使用 redux
- 使用React + Redux + React-router构建可扩展的前端应用
- 如何优雅地在React项目中使用Redux
- react系列(五)在React中使用Redux