From 92abaa5acc001db567e3f8cbcfa281edcbb93bc2 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Fri, 2 Sep 2022 15:48:02 +0800 Subject: [PATCH] Redux --- redux/src/components/app.jsx | 21 +++++++++++++++ redux/src/components/number.jsx | 35 +++++++++++++++++++++++++ redux/src/components/string.jsx | 45 +++++++++++++++++++++++++++++++++ redux/src/index.js | 37 ++++++++------------------- 4 files changed, 112 insertions(+), 26 deletions(-) create mode 100644 redux/src/components/app.jsx create mode 100644 redux/src/components/number.jsx create mode 100644 redux/src/components/string.jsx diff --git a/redux/src/components/app.jsx b/redux/src/components/app.jsx new file mode 100644 index 0000000..ee72616 --- /dev/null +++ b/redux/src/components/app.jsx @@ -0,0 +1,21 @@ +import React, { Component } from 'react'; +import Number from './number'; +import String from './string'; +import 'bootstrap/dist/css/bootstrap.css'; + +class App extends Component { + state = { } + render() { + return ( + +
+ +
+ +
+
+ ); + } +} + +export default App; \ No newline at end of file diff --git a/redux/src/components/number.jsx b/redux/src/components/number.jsx new file mode 100644 index 0000000..f20258d --- /dev/null +++ b/redux/src/components/number.jsx @@ -0,0 +1,35 @@ +import React, { Component } from 'react'; +import {connect} from 'react-redux'; +class Number extends Component { + state = { } + handleClick = () => { + this.props.concat('h'); + } + render() { + console.log(this.props) + return ( + +

Number组件:

+
{this.props.number}
+ +
+ ); + } +} + +const mapStateToProps = (state,props) => { + return { + number: state.number, + } +} + +const mapDispatchToProps = { + concat: (c) => { + return { + type: 'concat', + character: c, + } + } +} + +export default connect(mapStateToProps,mapDispatchToProps)(Number); \ No newline at end of file diff --git a/redux/src/components/string.jsx b/redux/src/components/string.jsx new file mode 100644 index 0000000..c9fa2bf --- /dev/null +++ b/redux/src/components/string.jsx @@ -0,0 +1,45 @@ +import React, { Component } from 'react'; +import { connect } from 'react-redux'; + +class String extends Component { + state = { } + handleAdd = () => { + this.props.add(10); + } + + handleSub = () => { + this.props.sub(20); + } + render() { + return ( + +

String组件:

+
{this.props.string}
+ + +
+ ); + } +} +const mapStateToProps = (state,props) => { + return { + string: state.string, + } +} + +const mapDispatchToProps = { + add: (n) => { + return { + type: 'add', + value: n, + } + }, + sub: (n) => { + return { + type: 'sub', + value: n, + } + } +} + +export default connect(mapStateToProps,mapDispatchToProps)(String); \ No newline at end of file diff --git a/redux/src/index.js b/redux/src/index.js index 46e3c06..d750822 100644 --- a/redux/src/index.js +++ b/redux/src/index.js @@ -3,9 +3,12 @@ import ReactDOM from 'react-dom/client'; import './index.css'; import { configureStore } from '@reduxjs/toolkit'; import { combineReducers } from '@reduxjs/toolkit'; +import App from './components/app.jsx'; +import { Provider } from 'react-redux'; + // 数字操作 -const f1 = (state = 0, action) => { +const f1 = (state = 1, action) => { switch (action.type) { case 'add': return state + action.value; @@ -17,27 +20,19 @@ const f1 = (state = 0, action) => { } // 字符操作 -const f2 = (state='',action) => { - switch(action.type) { +const f2 = (state = 'hhh', action) => { + switch (action.type) { case 'concat': return state + action.character; default: return state; } } -// // 组合f1和f2,f1和f2作为叶子节点 -// const f3 = (state={},action) => { -// return { -// f1: f1(state.f1,action), -// f2: f2(state.f2,action), -// } -// } -// 和上述函数的作用相同 const f3 = combineReducers({ - f1: f1, - f2: f2, + number: f1, + string: f2, }); @@ -45,22 +40,12 @@ const store = configureStore({ reducer: f3, }) -store.subscribe(() => {console.log(store.getState())}) - -store.dispatch({type: "add", value: 10}) -store.dispatch({type: "add", value: 10}) -store.dispatch({type: "add", value: 20}) -store.dispatch({type: "add", value: 20}) -store.dispatch({type: "sub", value: 100}) -store.dispatch({type: "sub", value: 100}) - -store.dispatch({type: "concat", character: "hhh"}) -store.dispatch({type: "concat", character: "ccc"}) const root = ReactDOM.createRoot(document.getElementById('root')); root.render( - - + + + ); \ No newline at end of file