目录:
redux是什么?
什么情况下需要使用redux?
redux工作流程:
redux的三个核心概念
action
reducer
store
Count.jsx
import React, {Component} from 'react';
class Count extends Component {
state = {count: 0}
increment = () => {
const {value} = this.selectNumber
const {count} = this.state
this.setState({count: count + value * 1})
}
decrement = () => {
const {value} = this.selectNumber
const {count} = this.state
this.setState({count: count - value * 1})
}
incrementIfOdd = () => {
const {value} = this.selectNumber
const {count} = this.state
if (count % 2 !== 0) {
this.setState({count: count + value * 1})
}
}
incrementAsync = () => {
const {value} = this.selectNumber
const {count} = this.state
setTimeout(() => {
this.setState({count: count + value * 1})
}, 500)
}
render() {
return (
当前求和为:{this.state.count}
);
}
}
export default Count;
Count.jsx
import React, {Component} from 'react';
import store from '../../redux/store'
class Count extends Component {
state = {carName: '奔驰c63'}
// componentDidMount() {
// store.subscribe(() => {
// this.setState({})
// })
// }
increment = () => {
const {value} = this.selectNumber
store.dispatch({type: 'increment', data: value * 1})
}
decrement = () => {
const {value} = this.selectNumber
store.dispatch({type: 'decrement', data: value * 1})
}
incrementIfOdd = () => {
const {value} = this.selectNumber
const count = store.getState()
if (count % 2 !== 0) {
store.dispatch({type: 'increment', data: value * 1})
}
}
incrementAsync = () => {
const {value} = this.selectNumber
setTimeout(() => {
store.dispatch({type: 'increment', data: value * 1})
}, 500)
}
render() {
return (
车名字:{this.state.carName}
当前求和为:{store.getState()}
);
}
}
export default Count;
store.js
import {createStore} from 'redux'
import countReducer from './count_reducer'
export default createStore(countReducer)
count_reducer.js
const initState = 0
export default function countReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case 'increment':
return preState + data
case 'decrement':
return preState - data
default:
return preState
}
}
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
store.subscribe(() => {
root.render( );
})
新增文件:
Count.jsx
import React, {Component} from 'react';
import store from '../../redux/store'
import {createIncrementAction, createDecrementAction} from '../../redux/count_action'
class Count extends Component {
state = {carName: '奔驰c63'}
// componentDidMount() {
// store.subscribe(() => {
// this.setState({})
// })
// }
increment = () => {
const {value} = this.selectNumber
store.dispatch(createIncrementAction(value * 1))
}
decrement = () => {
const {value} = this.selectNumber
store.dispatch(createDecrementAction(value * 1))
}
incrementIfOdd = () => {
const {value} = this.selectNumber
const count = store.getState()
if (count % 2 !== 0) {
store.dispatch(createIncrementAction(value * 1))
}
}
incrementAsync = () => {
const {value} = this.selectNumber
setTimeout(() => {
store.dispatch(createIncrementAction(value * 1))
}, 500)
}
render() {
return (
车名字:{this.state.carName}
当前求和为:{store.getState()}
);
}
}
export default Count;
constant.js
export const INCREMENT = 'incrment'
export const DECREMENT = 'decrement'
count_action.js
import {DECREMENT, INCREMENT} from "./constant";
export const createIncrementAction = data => ({type: INCREMENT, data})
export const createDecrementAction = data => ({type: DECREMENT, data})
count_reducer.js
import {INCREMENT, DECREMENT} from "./constant";
const initState = 0
export default function countReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case INCREMENT:
return preState + data
case DECREMENT:
return preState - data
default:
return preState
}
}
store.js
import {createStore} from 'redux'
import countReducer from './count_reducer'
export default createStore(countReducer)
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
store.subscribe(() => {
root.render( );
})
Count.jsx
import React, {Component} from 'react';
import store from '../../redux/store'
import {createIncrementAction, createDecrementAction, createIncrementAsyncAction} from '../../redux/count_action'
class Count extends Component {
state = {carName: '奔驰c63'}
// componentDidMount() {
// store.subscribe(() => {
// this.setState({})
// })
// }
increment = () => {
const {value} = this.selectNumber
store.dispatch(createIncrementAction(value * 1))
}
decrement = () => {
const {value} = this.selectNumber
store.dispatch(createDecrementAction(value * 1))
}
incrementIfOdd = () => {
const {value} = this.selectNumber
const count = store.getState()
if (count % 2 !== 0) {
store.dispatch(createIncrementAction(value * 1))
}
}
incrementAsync = () => {
const {value} = this.selectNumber
// setTimeout(() => {
// store.dispatch(createIncrementAction(value * 1))
store.dispatch(createIncrementAsyncAction(value * 1, 500))
// }, 500)
}
render() {
return (
车名字:{this.state.carName}
当前求和为:{store.getState()}
);
}
}
export default Count;
count_action.js
import {DECREMENT, INCREMENT} from "./constant";
//同步action,就是指action的值为object类型的一般对象
export const createIncrementAction = data => ({type: INCREMENT, data})
export const createDecrementAction = data => ({type: DECREMENT, data})
//异步action,就是指action的值为函数
export const createIncrementAsyncAction = (data, time) => {
return (dispatch) => {
setTimeout(() => {
dispatch(createIncrementAction(data))
}, time)
}
}
store.js
import {createStore, applyMiddleware} from 'redux'
import countReducer from './count_reducer'
import thunk from 'redux-thunk'
export default createStore(countReducer, applyMiddleware(thunk))
components/Count/Count.jsx
import React, {Component} from 'react';
class Count extends Component {
state = {carName: '奔驰c63'}
increment = () => {
const {value} = this.selectNumber
this.props.add(value * 1)
}
decrement = () => {
const {value} = this.selectNumber
this.props.sub(value * 1)
}
incrementIfOdd = () => {
const {value} = this.selectNumber
if (this.props.count % 2 !== 0) {
this.props.add(value * 1)
}
}
incrementAsync = () => {
const {value} = this.selectNumber
this.props.addAsync(value * 1, 500)
}
render() {
return (
车名字:{this.state.carName}
当前求和为:{this.props.count}
);
}
}
export default Count;
containers/Count/Count.jsx
import CountUI from '../../components/Count/Count'
import {connect} from 'react-redux'
import {createDecrementAction, createIncrementAction, createIncrementAsyncAction} from "../../redux/count_action";
import {number} from "prop-types";
function mapStateToProps(state) {
return {count: state}
}
function mapDispatchToProps(dispatch) {
return {
add: (number) => {
dispatch(createIncrementAction(number))
},
sub: (number) => {
dispatch(createDecrementAction(number))
},
addAsync: (number, time) => {
dispatch(createIncrementAsyncAction(number, time))
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(CountUI)
constant.js
export const INCREMENT = 'incrment'
export const DECREMENT = 'decrement'
count_action.js
import {DECREMENT, INCREMENT} from "./constant";
//同步action,就是指action的值为object类型的一般对象
export const createIncrementAction = data => ({type: INCREMENT, data})
export const createDecrementAction = data => ({type: DECREMENT, data})
//异步action,就是指action的值为函数
export const createIncrementAsyncAction = (data, time) => {
return (dispatch) => {
setTimeout(() => {
dispatch(createIncrementAction(data))
}, time)
}
}
count_reducer.js
import {INCREMENT, DECREMENT} from "./constant";
const initState = 0
export default function countReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case INCREMENT:
return preState + data
case DECREMENT:
return preState - data
default:
return preState
}
}
store.js
import {createStore, applyMiddleware} from 'redux'
import countReducer from './count_reducer'
import thunk from 'redux-thunk'
export default createStore(countReducer, applyMiddleware(thunk))
App.js
import React, {Component} from 'react';
import Count from "./containers/Count/Count";
import store from "./redux/store";
class App extends Component {
render() {
return (
);
}
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
store.subscribe(() => {
root.render( );
})
containers/Count/Count.jsx
import CountUI from '../../components/Count/Count'
import {connect} from 'react-redux'
import {createDecrementAction, createIncrementAction, createIncrementAsyncAction} from "../../redux/count_action";
export default connect(
state => ({count: state}),
// (dispatch) => (
// {
// add: number => dispatch(createIncrementAction(number)),
// sub: number => dispatch(createDecrementAction(number)),
// addAsync: (number, time) => dispatch(createIncrementAsyncAction(number, time))
//
// })
{
add: createIncrementAction,
sub: createDecrementAction,
addAsync: createIncrementAsyncAction
}
)(CountUI)
App.js
import React, {Component} from 'react';
import Count from "./containers/Count/Count";
class App extends Component {
render() {
return (
);
}
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {Provider} from "react-redux";
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
);
containers/Count/Count.jsx
import React, {Component} from 'react';
import {connect} from 'react-redux'
import {createDecrementAction, createIncrementAction, createIncrementAsyncAction} from "../../redux/count_action";
class Count extends Component {
state = {carName: '奔驰c63'}
increment = () => {
const {value} = this.selectNumber
this.props.add(value * 1)
}
decrement = () => {
const {value} = this.selectNumber
this.props.sub(value * 1)
}
incrementIfOdd = () => {
const {value} = this.selectNumber
if (this.props.count % 2 !== 0) {
this.props.add(value * 1)
}
}
incrementAsync = () => {
const {value} = this.selectNumber
this.props.addAsync(value * 1, 500)
}
render() {
console.log(this.props)
return (
车名字:{this.state.carName}
当前求和为:{this.props.count}
);
}
}
export default connect(
state => ({count: state}),
{
add: createIncrementAction,
sub: createDecrementAction,
addAsync: createIncrementAsyncAction
}
)(Count)
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {Provider} from "react-redux";
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
);
App.js
import React, {Component} from 'react';
import Count from "./containers/Count/Count";
import Person from "./containers/Person/Person";
class App extends Component {
render() {
return (
);
}
}
export default App;
store.js
import {createStore, applyMiddleware, combineReducers} from 'redux'
import countReducer from './reducers/count'
import thunk from 'redux-thunk'
import personReducer from "./reducers/person";
const allReducer = combineReducers({
he: countReducer,
rens: personReducer
})
export default createStore(allReducer, applyMiddleware(thunk))
constant.js
export const INCREMENT = 'incrment'
export const DECREMENT = 'decrement'
export const ADD_PERSON = 'add_person'
reducers/person.js
import {ADD_PERSON} from "../constant";
const initState = [{id: '001', name: 'tom', age: 18}]
export default function personReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case ADD_PERSON:
return [data, ...preState]
default:
return preState;
}
}
reducers/count.js
import {INCREMENT, DECREMENT} from "../constant";
const initState = 0
export default function countReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case INCREMENT:
return preState + data
case DECREMENT:
return preState - data
default:
return preState
}
}
actions/person.js
import {ADD_PERSON} from '../constant'
export const createAddPersonAction = personObj => ({type: ADD_PERSON, data: personObj})
actions/count.js
import {DECREMENT, INCREMENT} from "../constant";
//同步action,就是指action的值为object类型的一般对象
export const createIncrementAction = data => ({type: INCREMENT, data})
export const createDecrementAction = data => ({type: DECREMENT, data})
//异步action,就是指action的值为函数
export const createIncrementAsyncAction = (data, time) => {
return (dispatch) => {
setTimeout(() => {
dispatch(createIncrementAction(data))
}, time)
}
}
Person.jsx
import React, {Component} from 'react';
import {nanoid} from "nanoid";
import {connect} from "react-redux";
import {createAddPersonAction} from "../../redux/actions/person";
class Person extends Component {
addPerson = () => {
const name = this.nameNode.value
const age = this.ageNode.value
const personObj = {id: nanoid(), name, age}
this.props.jiaYiRen(personObj)
this.nameNode.value = ''
this.ageNode.value = ''
}
render() {
return (
我是person组件,上方组件求和为{this.props.he}
this.nameNode = c} type="text" placeholder="输入名字"/>
this.ageNode = c} type="text" placeholder="输入名字"/>
{
this.props.yiduiren.map((p) => {
return - {p.name}--{p.age}
})
}
);
}
}
export default connect(
state => ({yiduiren: state.rens, he: state.he}),
{jiaYiRen: createAddPersonAction}
)(Person)
count.jsx
import React, {Component} from 'react';
import {connect} from 'react-redux'
import {createDecrementAction, createIncrementAction, createIncrementAsyncAction} from "../../redux/actions/count";
class Count extends Component {
increment = () => {
const {value} = this.selectNumber
this.props.add(value * 1)
}
decrement = () => {
const {value} = this.selectNumber
this.props.sub(value * 1)
}
incrementIfOdd = () => {
const {value} = this.selectNumber
if (this.props.count % 2 !== 0) {
this.props.add(value * 1)
}
}
incrementAsync = () => {
const {value} = this.selectNumber
this.props.addAsync(value * 1, 500)
}
render() {
return (
我是Count组件,下方组件总人数为:{this.props.renshu}
当前求和为:{this.props.count}
);
}
}
export default connect(
state => ({count: state.he, renshu: state.rens.length}),
{
add: createIncrementAction,
sub: createDecrementAction,
addAsync: createIncrementAsyncAction
}
)(Count)
项目结构:
运行结果:
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {Provider} from "react-redux";
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
);
App.js
import React, {Component} from 'react';
import Count from "./containers/Count/Count";
import Person from "./containers/Person/Person";
class App extends Component {
render() {
return (
);
}
}
export default App;
store.js
import {createStore, applyMiddleware, combineReducers} from 'redux'
import thunk from 'redux-thunk'
import {composeWithDevTools} from "redux-devtools-extension";
import allReducer from "./reducers";
export default createStore(allReducer, composeWithDevTools(applyMiddleware(thunk)))
constant.js
export const INCREMENT = 'incrment'
export const DECREMENT = 'decrement'
export const ADD_PERSON = 'add_person'
reducers/count.js
import {INCREMENT, DECREMENT} from "../constant";
const initState = 0
export default function countReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case INCREMENT:
return preState + data
case DECREMENT:
return preState - data
default:
return preState
}
}
reducers/index.js
import {combineReducers} from "redux";
import countReducer from "./count";
import personReducer from "./person";
const allReducer = combineReducers({
count: countReducer,
persons: personReducer
})
export default allReducer
reducers/person.js
import {ADD_PERSON} from "../constant";
const initState = [{id: '001', name: 'tom', age: 18}]
export default function personReducer(preState = initState, action) {
const {type, data} = action
switch (type) {
case ADD_PERSON:
return [data, ...preState]
default:
return preState;
}
}
actions/count.js
import {DECREMENT, INCREMENT} from "../constant";
//同步action,就是指action的值为object类型的一般对象
export const increment = data => ({type: INCREMENT, data})
export const decrement = data => ({type: DECREMENT, data})
//异步action,就是指action的值为函数
export const incrementAsync = (data, time) => {
return (dispatch) => {
setTimeout(() => {
dispatch(increment(data))
}, time)
}
}
actions/person.js
import {ADD_PERSON} from '../constant'
export const addPerson = personObj => ({type: ADD_PERSON, data: personObj})
Person.jsx
import React, {Component} from 'react';
import {nanoid} from "nanoid";
import {connect} from "react-redux";
import {addPerson} from "../../redux/actions/person";
class Person extends Component {
addPerson = () => {
const name = this.nameNode.value
const age = this.ageNode.value * 1
const personObj = {id: nanoid(), name, age}
this.props.addPerson(personObj)
this.nameNode.value = ''
this.ageNode.value = ''
}
render() {
return (
我是person组件,上方组件求和为{this.props.count}
this.nameNode = c} type="text" placeholder="输入名字"/>
this.ageNode = c} type="text" placeholder="输入名字"/>
{
this.props.persons.map((p) => {
return - {p.name}--{p.age}
})
}
);
}
}
export default connect(
state => ({persons: state.persons, count: state.count}),
{addPerson}
)(Person)
Count.jsx
import React, {Component} from 'react';
import {connect} from 'react-redux'
import {
decrement,
increment,
incrementAsync
} from "../../redux/actions/count";
class Count extends Component {
increment = () => {
const {value} = this.selectNumber
this.props.increment(value * 1)
}
decrement = () => {
const {value} = this.selectNumber
this.props.decrement(value * 1)
}
incrementIfOdd = () => {
const {value} = this.selectNumber
if (this.props.count % 2 !== 0) {
this.props.increment(value * 1)
}
}
incrementAsync = () => {
const {value} = this.selectNumber
this.props.incrementAsync(value * 1, 500)
}
render() {
return (
我是Count组件,下方组件总人数为:{this.props.personCount}
当前求和为:{this.props.count}
);
}
}
export default connect(
state => ({count: state.count, personCount: state.persons.length}),
{
increment,
decrement,
incrementAsync
}
)(Count)
1.下载依赖
2.build
3.启动服务器:
4.运行结果:
https://gitee.com/coderPatrickStar/react/tree/master/20231003