1.搭建redux结构

image.png

新建store文件

  1. import {createStore} from 'redux';
  2. import reducer from './reducer';
  3. const store = createStore(
  4. reducer
  5. )
  6. export default store

新建reducer文件

  1. const defalutState = {
  2. inputValue:'hello world',
  3. list:[]
  4. }
  5. export default (state=defalutState,action)=>{
  6. return state
  7. }

组件

  1. import React, { Component } from "react";
  2. import store from './store/store';
  3. class TodoList extends Component {
  4. constructor(props){
  5. super(props)
  6. this.state = store.getState()
  7. }
  8. render() {
  9. return (
  10. <div>
  11. <div>
  12. <input value={this.state.inputValue}/>
  13. <button>提交</button>
  14. </div>
  15. <ul>
  16. <li>test</li>
  17. </ul>
  18. </div>
  19. );
  20. }
  21. }
  22. export default TodoList;

2.在index文件中引入Provider(提供器)

引入**Provider后所有被Provider**包裹的组件(可包裹多个组件)都可以使用store中的state

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import TodoList from "./TodoList";
  4. import { Provider } from "react-redux";
  5. import store from "./store/store";
  6. ReactDOM.render(
  7. <React.StrictMode>
  8. <Provider store={store}>
  9. <TodoList />
  10. </Provider>
  11. </React.StrictMode>,
  12. document.getElementById("root")
  13. );

注意:Provider不可以使用变量包裹,否则会报错如👇

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import TodoList from "./TodoList";
  4. import { Provider } from "react-redux";
  5. import store from "./store/store";
  6. const App = (
  7. <Provider store={store}>
  8. <TodoList />
  9. </Provider>
  10. );
  11. ReactDOM.render(
  12. <React.StrictMode>
  13. <App />
  14. </React.StrictMode>,
  15. document.getElementById("root")
  16. );

image.png

3.在组件中引入connect(连接器)

将原来export 出去的组件,通过connect方法连接store
connect方法中第一个参数为获取store中state的映射关系

  1. import React, { Component } from "react";
  2. import {connect} from 'react-redux';
  3. class TodoList extends Component {
  4. constructor(props){
  5. super(props)
  6. }
  7. render() {
  8. return (
  9. <div>
  10. <div>
  11. <input value={this.props.inputValue}/>
  12. <button>提交</button>
  13. </div>
  14. <ul>
  15. <li>test</li>
  16. </ul>
  17. </div>
  18. );
  19. }
  20. }
  21. const stateToProps = (state)=>{
  22. return{
  23. inputValue:state.inputValue
  24. }
  25. }
  26. export default connect(stateToProps,null)(TodoList);

connect方法中第二个参数为修改store中state的映射关系

  1. import React, { Component } from "react";
  2. import {connect} from 'react-redux';
  3. class TodoList extends Component {
  4. render() {
  5. return (
  6. <div>
  7. <div>
  8. <input
  9. value={this.props.inputValue}
  10. onChange={this.props.inputChange}/>
  11. <button>提交</button>
  12. </div>
  13. <ul>
  14. <li>test</li>
  15. </ul>
  16. </div>
  17. );
  18. }
  19. }
  20. const stateToProps = (state)=>{
  21. console.log(state)
  22. return{
  23. inputValue:state.inputValue
  24. }
  25. }
  26. const dispatchToProps=(dispatch)=>{
  27. return {
  28. inputChange(e){
  29. let action = {
  30. type:'change_input',
  31. value:e.target.value
  32. }
  33. dispatch(action)
  34. }
  35. }
  36. }
  37. export default connect(stateToProps,dispatchToProps)(TodoList);

注意:还要修改reducer中的业务逻辑

完整代码

组件:TodoList.js

  1. import React from "react";
  2. import { connect } from "react-redux";
  3. const TodoList = (props) => {
  4. const { inputValue, inputChange, clickBtn, list } = props;
  5. return (
  6. <div>
  7. <div>
  8. <input value={inputValue} onChange={inputChange} />
  9. <button onClick={clickBtn}>提交</button>
  10. </div>
  11. <ul>
  12. {list.map((value, index, array) => (
  13. <li key={index}>{value}</li>
  14. ))}
  15. </ul>
  16. </div>
  17. );
  18. };
  19. const stateToProps = (state) => {
  20. console.log(state);
  21. return {
  22. inputValue: state.inputValue,
  23. list: state.list,
  24. };
  25. };
  26. const dispatchToProps = (dispatch) => {
  27. return {
  28. inputChange(e) {
  29. let action = {
  30. type: "change_input",
  31. value: e.target.value,
  32. };
  33. dispatch(action);
  34. },
  35. clickBtn() {
  36. console.log(1111);
  37. let action = {
  38. type: "add_item",
  39. };
  40. dispatch(action);
  41. },
  42. };
  43. };
  44. export default connect(stateToProps, dispatchToProps)(TodoList);

入口文件:index.js

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import TodoList from "./TodoList";
  4. import { Provider } from "react-redux";
  5. import store from "./store/store";
  6. ReactDOM.render(
  7. <React.StrictMode>
  8. <Provider store={store}>
  9. <TodoList />
  10. </Provider>
  11. </React.StrictMode>,
  12. document.getElementById("root")
  13. );

redux:reducer.js

  1. const defalutState = {
  2. inputValue:'hello world',
  3. list:[]
  4. }
  5. export default (state=defalutState,action)=>{
  6. if(action.type === 'change_input'){
  7. let newState = JSON.parse(JSON.stringify(state))
  8. newState.inputValue = action.value
  9. return newState
  10. }
  11. if(action.type === 'add_item'){
  12. let newState = JSON.parse(JSON.stringify(state))
  13. newState.list.push(newState.inputValue)
  14. newState.inputValue=''
  15. return newState
  16. }
  17. return state
  18. }

redux:store.js

  1. import {createStore} from 'redux';
  2. import reducer from './reducer';
  3. const store = createStore(
  4. reducer
  5. )
  6. export default store