First, you need to install ANTD and Redux to initialize the project
1. Characters in Redux
-
🚀 Store:
- Used to maintain the application
state
; createStore()
To create astore
;- provide
getState()
Methods to obtainstate
; - provide
dispatch(action)
Methods the updatestate
; - through
subscribe(listener)
Register listeners; - through
subscribe(listener)
The returned function unlogs the listener.
- Used to maintain the application
-
🚀 Reducer:
- How does the specified application state respond to changes
actions
And sent tostore
the
- How does the specified application state respond to changes
-
🚀 Action:
- Transfer data from application to application
store
The payload of
- Transfer data from application to application
Example 2. Story:
Directory structure:
SRC ├ ─ App. CSS ├ ─ App. Js ├ ─ index. The CSS ├ ─ index. The js ├ ─ logo. The SVG ├ ─ store | └ index. The js ├ ─ Components | └ ReduxTest. JsCopy the code
The code is 👇 :
🚀 store
In theIndex. Js:
- To create the
reducer
To deal with specific operations - will
reducer
Pure functions are passed as arguments tocreateStore()
- export
store
;
/ / 🚀 SRC/store/index. Js
import {createStore} from "redux";
/ / create a reducer
function conter(state=0,action){
switch (action.type){
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
default:
returnstate; }}/ / create a store
const store = createStore(conter);
export default store;
Copy the code
🚀 src
In theindex.js
:
- To create a
render
Function for rendering - will
render
The render function is passed as an argument tostore.subscribe(render)
- So that if
stroe
In thestate
If there is a change, then it will be implementedrender
The function completes the re-rendering, updating the page view
- So that if
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import store from "./store";
import ReduxTest from "./Components/ReduxTest";
function render(){
ReactDOM.render(
<ReduxTest/>.document.getElementById('root')); } render();// 🚀 sign up to subscribe to render again every time state is updated
store.subscribe(render)
Copy the code
🚀 ReduxTest.js
:
store.getState()
To obtainredux
In thestate
– store.dispatch() Dispatches an action
store
Got thisaction
Along with the previousstate
All together.reducer
In thereducer
To go toaction.type
Do the processing and return a new onestate
tostore
store
Update page data
// Components/ReduxTest.js
import React, {Component} from 'react';
import store from ".. /store";
import {Button} from "antd";
class ReduxTest extends Component {
constructor(props) {
super(props);
this.increment = this.increment.bind(this);
this.decrement = this.decrement.bind(this);
}
increment(){
store.dispatch({type:"INCREMENT"})}decrement(){
store.dispatch({type:"DECREMENT"})}render() {
return (
<div>
<p>{store.getState()}</p>
<Button type="success" onClick={this.increment}> + 1 </Button>
<Button type="primary" onClick={this.decrement}> - 1 </Button>
</div>); }}export default ReduxTest;
Copy the code
At the heart of the Redux architecture's design: strict one-way data flow
Problem: Every state update rerenders, causing unnecessary duplication in large applications.
React-redux is recommended if you want to use redux more elegantly