Store

  1. 1.需要将多个reducer和并放在一个对象中
  2. 2.引入combineReducers
  3. 3.通过调用combineReducers,入参一个对象,这里的key可以自己命名,value分别是引入的reducer
  1. //该文件用于暴露store对象
  2. //引入createStore,专门用于创建redux中最核心的store对象
  3. //引入applyMiddleware中间件
  4. import {createStore,applyMiddleware,combineReducers} from 'redux'
  5. //引入为count组件服务的reducer
  6. import countReducer from './reducers/count'
  7. import personReducer from './reducers/person'
  8. //引入react-thunk,用于支持异步action
  9. import thunk from 'redux-thunk'
  10. //汇总reducer变成一个总的reducer
  11. const allReducer=combineReducers({
  12. he:countReducer,
  13. rens:personReducer
  14. })
  15. //store中需要一个为之服务的reducer,因此将引入的reducer当参数传入这个创建的store中
  16. //并且将这个store对象暴露出去
  17. //
  18. export default createStore(allReducer,applyMiddleware(thunk)

Action

创建一个对象,有两个参数,第一个参数是type,第二个参数是需要传递过来的数据data

count_action

  1. import { INCREMENT, DECREMENT} from '../constant'
  2. export const createIncrement=data=>
  3. ({
  4. type:INCREMENT,
  5. data
  6. })
  7. export const createDecrement=data=>(
  8. {
  9. type:DECREMENT,
  10. data
  11. }
  12. )
  13. //异步action
  14. export const createAsyncIncrement=(data,time)=>{
  15. return dispatch=>{
  16. setTimeout(() => {
  17. dispatch(incrementAction(data))
  18. }, time);
  19. }
  20. }

person_action

  1. import{ADD_PERSON}from '../constant'
  2. export const createPerson=personObj=>({
  3. type:ADD_PERSON,
  4. data:personObj
  5. })

Constant

存放常量

  1. //暴露加法和减法
  2. export const INCREMENT='increment'
  3. export const DECREMENT='decrement'
  4. export const ADD_PERSON='add_crement'

Reducers

count_reducer

做一些逻辑处理,这里的函数接受两个参数,第一个参数是之前的值,第二个参数是外界传递过来的action, action是一个对象,对象的参数是type与data

  1. import { INCREMENT, DECREMENT} from '../constant'
  2. const initState=0
  3. export default function countReducer(preState = initState, action) {
  4. const {
  5. type,
  6. data
  7. } = action
  8. switch (type) {
  9. case INCREMENT:
  10. return preState + data
  11. case DECREMENT:
  12. return preState - data
  13. default:
  14. return preState
  15. }
  16. }

person_reducer

  1. import{ADD_PERSON}from '../constant'
  2. let person=[{id:"001",name:"ws",age:23}]
  3. export default function PersonSum(preState=person,action){
  4. let {type,data}=action
  5. switch (type) {
  6. case ADD_PERSON:
  7. return [data,...preState]
  8. default:
  9. return preState
  10. }
  11. }

组件中使用

这里的组件有两个,一个是ui组件一个是容器组件,ui组件主要进行展示,容器组件做一些数据加工,容器组件包裹ui组件,并将容器组件暴露出去

login组件

  1. import{ createIncrement,createDecrement, createAsyncIncrement} from '../../redux/actions/count'
  2. //引入store
  3. // import store from '../../redux/count_redux/store'
  4. //引入connect用于连接ui组件和redux
  5. import {connect} from 'react-redux'
  6. import React, { Component } from 'react'
  7. class index extends Component {
  8. //加
  9. increment=()=>{
  10. let {value}=this.selectNumber
  11. this.props.jia(value*1)
  12. }
  13. decrement=()=>{
  14. let {value}=this.selectNumber
  15. this.props.jian(value*1)
  16. }
  17. //异步加
  18. asyncIcrement=()=>{
  19. let {value}=this.selectNumber
  20. this.props.jiaAsync(value*1,500)
  21. }
  22. //奇数加
  23. oddIcrement=()=>{
  24. let {value}=this.selectNumber
  25. if(this.props.count%2!=0){
  26. this.props.jia(value*1)
  27. }
  28. }
  29. render() {
  30. return (
  31. <div>
  32. <h1>person组件下方总人数{this.props.renshu}</h1>
  33. <h2>当前显示的数是:{this.props.count}</h2>
  34. <select ref={c=>this.selectNumber=c}>
  35. <option value="1">1</option>
  36. <option value="2">2</option>
  37. <option value="3">3</option>
  38. </select>
  39. <button onClick={this.increment}>加</button>&nbsp;&nbsp;&nbsp;
  40. <button onClick={this.decrement}>减</button>&nbsp;&nbsp;&nbsp;
  41. <button onClick={this.asyncIcrement}>异步加</button>&nbsp;&nbsp;&nbsp;
  42. <button onClick={this.oddIcrement}>奇数加</button>&nbsp;&nbsp;&nbsp;
  43. </div>
  44. )
  45. }
  46. }
  47. //高阶函数,连接ui组件
  48. //使用connect创建并暴露一个math组件
  49. //connect第一个函数是映射ui组件和容器组件的关系,第二个组件是连接ui组件和容器组件
  50. export default connect(
  51. state=>({
  52. count:state.he,
  53. renshu:state.rens.length
  54. })
  55. //在react-redux中可以将action自动dispatch给redux,
  56. //如果两个参数都是函数,第二个参数可以写成一个对象
  57. ,{
  58. jia:createIncrement,
  59. jian:createDecrement,
  60. jiaAsync:createAsyncIncrement
  61. })(index)

容器组件主要是通过connect进行使用 1.首先需要下载react-redux,yarn add react-redux 2.引入connect 3.connect中可以调用两次,第一次调用可以传递两个函数,第一个函数返回一个对象,对象的值可以通过state.去获取reducer的值,第二个函数返回一个对象,key是我们自己定义的,value是函数。 4.这里的函数可以是处理数据的action,action需要引入进来 5.connect的第二次调用需要将ui组件传递进去

register组件

下载nanoid ,生成一个随机且不重复的id

  1. yarn add nanoid
  1. import React, { Component } from 'react'
  2. import {nanoid} from 'nanoid'
  3. import {connect} from 'react-redux'
  4. import {createPerson}from "../../redux/actions/person"
  5. class index extends Component {
  6. addClick=()=>{
  7. const nameValue=this.nameNode.value
  8. const ageValue=this.ageNode.value
  9. const personObj={id:nanoid(),name:nameValue,age:ageValue}
  10. this.props.jiayiren(personObj)
  11. this.nameNode.value=""
  12. this.ageNode.value=""
  13. }
  14. render() {
  15. console.log(this.props.yiduiren,"gggg")
  16. return (
  17. <div>
  18. <h1>上方显示的和为{this.props.sum}</h1>
  19. <input type="text" placeholder="请输入姓名" ref={c=>this.nameNode=c}/>
  20. <input type="text" placeholder="请输入年龄" ref={c=>this.ageNode=c}/>
  21. <button onClick={this.addClick}>添加</button>
  22. <ul>
  23. {
  24. this.props.yiduren.map(p=>{
  25. return <li key={p.id}>姓名:{p.name}===年龄:{p.age}</li>
  26. })
  27. }
  28. </ul>
  29. </div>
  30. )
  31. }
  32. }
  33. export default connect(
  34. state=>(
  35. {
  36. yiduren:state.rens,
  37. sum:state.he
  38. }),{
  39. jiayiren:createPerson
  40. }
  41. )(index)

index.js

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Provider } from 'react-redux';
  4. import App from './App';
  5. import store from './redux/store'
  6. ReactDOM.render(
  7. // React.StrictMode检查app和app包裹的组件是否合理
  8. <Provider store={store}>
  9. <App />
  10. </Provider>
  11. ,
  12. document.getElementById('root')
  13. );

app.js

  1. import Login from './components/Login'
  2. import Register from "./components/Register"
  3. function App() {
  4. return (
  5. <div>
  6. <Login/>
  7. <hr/>
  8. <Register></Register>
  9. </div>
  10. );
  11. }
  12. export default App;