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 ()

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e66056-45b7d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e66056-45b7d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?