React Redux is the official React binding for Redux. It allows the React component to read data from the Redux store and dispatch operations to the store to update the data. Before we learn react-Redux, we’re going to finish redux. Redux redux redux redux redux redux redux redux redux redux (even though the man even
//reactReduc_count.js
// Connect is used to connect the React component to the Redux store
import {connnect} from 'react-redux'
// The component that needs to be wrapped
import countUI from '.. /component/Count'
/ / into actions
import {Add,Subtract} from '.. /redux/actions'
// Return the initial value with the default parameter state
function mapStateToProps(state){
return {count:state}
}
// The default argument is dispatch, so that the corresponding action can be used in this.porps.add
function mapDispatchToProps(dispatch){
return{
'add':(data) = >{dispatch(Add(data))}
'subtract':(data) = >{dispatch(Subtract(data))}
}
}
// This mapDispatchToProps can also be abbreviated to objects
const mapDispatchToProps = {
'add':Add,
'subtract':Subtract
}
// Using connect and exporting, a new component is generated, but store is passed in
export default connect(mapStateToProps,mapDispatchToProps)(countUI)
Copy the code
//countUI.js
import React,{component} from 'react'
export default class countUI extends Component{
return() {<div>{/* Get state */}<h1>this.porps.count</h1>
<button onClick={this.add} ></button>
<button onClick={this.subtract}></button>
</div>
}
// Add method
add=() = >{
/ / 1
this.porps.add(1)}// subtraction method
subtract=() = >{
/ / minus 1
this.porps.subtract(1)}}Copy the code
//acitons.js
export function Add(data){
return {type:'add',data}
}
export function Subtract(data){
return {type:'subtract',data}
}
Copy the code
//store.js
// createStore is introduced to create a stroe
import {createStore} from 'redux'
//createStore needs to pass a reducer
import countReducer from './count_reducer'
/ / export
export default createStore(countReducer)
Copy the code
Reudx has combineReducers() that can combine multiple reducers
//store.js
// createStore is introduced to create a stroe
import {createStore,combineReducers} from 'redux'
//createStore needs to pass a reducer
import countReducer from './reducer/count'
// Pass in the second component
import peopleReducer from './reducer/people'
// Merge the reducer
const allReducer = combineReducers({
sum:countReducer,
person:peopleReducer
})
/ / export
export default createStore(allReducer)**
Copy the code
Using the component
//App.js
import React, { Component } from 'react'
import Count from './react_redux/reactReduc_count'
import store from './redux/store'
export default class App extends Component {
render() {
return (
<div>
<Count store={store}/>
</div>)}}Copy the code
If the store is imported the way above, each component to be used is passed in one by one, which is too cumbersome, so you can use the following method
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import store from './redux/store';
import {Provider} from 'react-redux'
ReactDOM.render(
<Provider store={stroe}>
<App />
</Provider>.document.getElementById('root'));// No need to pass it in one by one
Copy the code
Reactreduc_count.js and countui.js can be written as one file
//connect is used to create the container parent
import { connect } from 'react-redux'
import { Plus, Subtract } from '.. /redux/action/count'
import React, { Component } from 'react'
class Count extends Component {
render() {
return (
<div>
<h1>{this.props.count}</h1>
<button onClick={this.add}>+</button>
<button onClick={this.subtract}>-</button>
</div>)}/ / 1
add = () = > {
this.props[PLUS](this.state.selectN)
}
/ / minus 1
subtract = () = > {
this.props[SUBTRACT](this.state.selectN)
}
}
function mapStateToProps(state) {
return {
count: state.sum,
peopleArr: state.man.length
}
}
export default connect(mapStateToProps, {
'add': Add,
'subtract': Subtract
})(Count)
Copy the code