MapStateToProps cannot be injected into props? in React-redux

problem description

to realize the communication between react components through react-redux and redux, reducer, action and store are all written correctly, and mapDispatchToProps can also pass values correctly. Only the function of mapStateToProps is incorrect, and the state value in it will be changed according to the value passed, but the this.props printed in render only has the value of the first time, and then the input value will not be rendered to props

.

Screenshot

  1. enter content for the first time: Props has content
    clipboard.png
  2. : Props,Render
    clipboard.png
    Props
    clipboard.png

related codes

//mapStateToProps()
function mapStateToProps(state){
    console.log(state.user);
    return {
        users:state.user
    }
}

AppContent = connect(mapStateToProps)(AppContent)

export default AppContent;
//reducer.js
let infoBox = [];

function put_in_infoBox(action){
    infoBox.push(action);   //infoBox
}

function sendMsg(state,action){
    if(!state){
        //
        return {
            user:{}
        }
    }

    switch(action.type){
        //action.typeaction.js,dispatch
        case "SEND":
            //
            put_in_infoBox(action);
            console.log(infoBox);
            return {
                user:infoBox
            }
        
        default:
            return state;
    }
}

export default sendMsg
//store.js
//
import reducer from "./reducer"

import { createStore } from "redux" 

const store = createStore(
    reducer,
)

export default store
export const sendMsgAction = ({name,message}) => ({
    type:"SEND",
    name,
    message
})
//mapDispatchToProps()

const mapDispatchToProps = (dispatch)  => {
    return {
        //
        onSendMsg({name,message}){
            // console.log(message);
            dispatch(sendMsgAction({name,message}));
        },

    }
}

AppFoot = connect(
    null,
    mapDispatchToProps
)(AppFoot)

export default AppFoot;

effect

  1. should have an effect: this.props is the value of mapStateToProps after each change of state
  2. actual effect: this.props currently displays only the first group of incoming data
Apr.09,2021

The reference to

infoBox is always the same and is dropped by Filter by react-redux 's built-in shallow compare.

just change the reference of infoBox. [.infobox] or infoBox.slice () can


reducer use combineReducers () to process your reducer before giving it to createStore ()

Menu