• reducer 对数据进行操作

创建counter.js

  1. // reducer是个纯函数, 改变store数据的
  2. const counter = (state = 0, action) => {
  3. switch(action.type) {
  4. case 'increment':
  5. return state+1
  6. case 'decrement':
  7. return state - 1
  8. default:
  9. return state
  10. }
  11. }
  12. export default counter
  • index.js 引用
  • 参考 ```javascript import {createStore } from ‘redux’ import counter from ‘./reducer/counter’ import App from ‘./App’;

// 创建仓库 const storeCounter = createStore(counter) // 监听数据变化 storeCounter.subscribe(() => { render() //监听变化页面就重新渲染, // console.log(‘store’, storeCounter.getState()) // 获取state })

const render = () => { ReactDOM.render( storeCounter.dispatch({type:’increment’})} onDecrement={() => storeCounter.dispatch({type:’decrement’})} val={storeCounter.getState()} />, document.getElementById(‘root’) ); } render()

  1. ---
  2. <a name="mCnpp"></a>
  3. ### react-redux
  4. - 安装 cnpm i --save-dev react-redux
  5. <a name="NjM3q"></a>
  6. #### 主文件 index.js 中做关联
  7. ```javascript
  8. // 包裹用的
  9. import {Provider} from 'react-redux'
  10. import counter from './reducer/counter'
  11. import {createStore } from 'redux'
  12. // 创建仓库
  13. const storeCounter = createStore(counter)
  14. ReactDOM.render(
  15. <Provider store={storeCounter}>
  16. <App/>
  17. </Provider>,
  18. document.getElementById('root')
  19. );
  • App.js 中
  1. import React from 'react';
  2. // 把 该组件与 redux 链接起来
  3. import {connect} from 'react-redux'
  4. class App extends React.Component {
  5. render() {
  6. console.log(this.props)
  7. const {increment, decrement} = this.props
  8. return (
  9. <div className='container'>
  10. <h1 className='jumbotron-heading text-center'>{this.props.counter}</h1>
  11. <p className='text-center'>
  12. <button onClick={() => increment()} className='btn-primary btn'>增加</button>
  13. <button onClick={() => decrement()} className='btn-default btn ml-3'>减少</button>
  14. </p>
  15. </div>
  16. )
  17. }
  18. }
  19. const mapStateToProps = (state) => {
  20. return {
  21. counter: state
  22. }
  23. }
  24. // action 触发注入
  25. const mapDispatchToProps = (dispatch) => {
  26. return {
  27. increment: () => {dispatch(increment())}, //内部方法执行
  28. decrement: () => {dispatch(decrement())}
  29. }
  30. }
  31. // 高阶组件 , 把状态和props 关联起来 , state 和action 顺序不能改
  32. export default connect(mapStateToProps, mapDispatchToProps)(App)

优化 bindActionCreators

  1. import React from 'react';
  2. // 把 该组件与 redux 链接起来
  3. import {connect} from 'react-redux'
  4. // 引入
  5. import * as counterActions from './actions/counter'
  6. import {bindActionCreators} from 'redux'
  7. class App extends React.Component {
  8. render() {
  9. // console.log(this.props)
  10. return (
  11. <div className='container'>
  12. <h1 className='jumbotron-heading text-center'>{this.props.counter}</h1>
  13. <p className='text-center'>
  14. // 使用方式
  15. <button onClick={() => this.props.counterActions.increment()} className='btn-primary btn'>增加</button>
  16. <button onClick={() => this.props.counterActions.decrement()} className='btn-default btn ml-3'>减少</button>
  17. </p>
  18. </div>
  19. )
  20. }
  21. }
  22. // this.props.counter 使用
  23. const mapStateToProps = (state) => {
  24. return {
  25. counter: state
  26. }
  27. }
  28. // action 触发注入
  29. // const mapDispatchToProps = (dispatch) => {
  30. // return {
  31. // increment: () => {dispatch(increment())},
  32. // decrement: () => {dispatch(decrement())}
  33. // }
  34. // }
  35. // this.props.counteActions 使用
  36. const mapDispatchToProps = (dispatch) => {
  37. return {
  38. counterActions: bindActionCreators(counterActions, dispatch)
  39. }
  40. }
  41. // 高阶组件 state和action 书序不能颠倒
  42. export default connect(mapStateToProps, mapDispatchToProps)(App)
  • 优化 数据归类

创建 actions/counter.js

  1. import * as constants from '../constants'
  2. export const increment = (num) => {
  3. return {
  4. type: constants.INCREMENT,
  5. num
  6. }
  7. }
  8. export const decrement = (num) => {
  9. return {
  10. type: constants.DECREMENT,
  11. num
  12. }
  13. }

常量文件 constants/index.js

  1. // 常量 保存
  2. export const INCREMENT = "INCREMENT"
  3. export const DECREMENT = "DECREMENT"

reducer/counter.js

  1. import * as constants from '../constants'
  2. // reducer是个纯函数, 改变store数据的
  3. // 数据在action 中
  4. const counter = (state = 0, action) => {
  5. switch(action.type) {
  6. case constants.INCREMENT:
  7. return state + action.num
  8. case constants.DECREMENT:
  9. return state - action.num
  10. default:
  11. return state
  12. }
  13. }
  14. export default counter
  • 数据调用可以传参
  1. <button onClick={() => this.props.counterActions.increment(10)} className='btn-primary btn'>增加</button>
  2. <button onClick={() => this.props.counterActions.decrement(5)} className='btn-default btn ml-3'>减少</button>

reducer 合并

创建 reducer/index.js

  1. import {combineReducers} from 'redux'
  2. import counter from './counter'
  3. const rootReducer = combineReducers({
  4. counter
  5. })
  6. export default rootReducer // 是个对象,组件调用的时候用对象
  1. const mapStateToProps = (state) => {
  2. return {
  3. counter: state.counter ////////这里 对象形式
  4. }
  5. }

新增user 操作

  1. // actions/user.js
  2. import {ADD_USER} from '../constants'
  3. export function addUser(name) {
  4. return {
  5. type: ADD_USER,
  6. name
  7. }
  8. }
  9. // constants/index.js
  10. // user
  11. export const ADD_USER = "ADD_USER"
  12. // reducer/user.js
  13. import {ADD_USER} from '../constants'
  14. // reducer是个纯函数, 改变store数据的
  15. // 数据在action 中
  16. // 改变对象
  17. const counter = (state = {name:'测不准'}, action) => {
  18. switch(action.type) {
  19. case ADD_USER:
  20. state.name = action.name
  21. // 对象要创建个新对象
  22. let newState = Object.assign({},state,action)
  23. return newState
  24. default:
  25. return state
  26. }
  27. }
  28. export default counter
  29. 使用
  30. const mapStateToProps = (state) => {
  31. console.log(123, state)
  32. return {
  33. counter: state.counter,
  34. user: state.user
  35. }
  36. }
  37. const mapDispatchToProps = (dispatch) => {
  38. return {
  39. counterActions: bindActionCreators(counterActions, dispatch),
  40. userActions: bindActionCreators(userActions, dispatch)
  41. }
  42. }