【问题标题】:Need to replace the list in react redux需要替换react redux中的列表
【发布时间】:2022-01-07 08:59:39
【问题描述】:

我正在尝试设置代码块不起作用的 userdetails 的属性。

import CHATS from '../actions/ChatMessages';
import MESSAGES from '../actions/Messages';
import SEARCH from '../actions/SearchUser';

const chatReducer = (state = {}, { type, payload }) => {
    switch (type) {
        case CHATS:
            return { ...state, ...payload };
        case SEARCH:
            return {
                ...state,
                userDetails: [...payload.userDetails]
            };
        case MESSAGES:
            let index = state.chats.findIndex(
                chat => chat.chatId === payload.chatId
            );
            if (index !== -1) {
                let conn = state.chats[index];
                const msg = payload.msg;
                conn.lastMsg = msg;
                const messages = [...conn.messages, payload];
                const newState = { ...state };
                newState.chats[index] = { ...conn, messages };
                console.log("state ", newState);
                return newState;
            }
            return state;
        default:
            return state
    };
};

export default chatReducer;

userdetails 字段没有替换值。状态对象有什么问题吗?

【问题讨论】:

  • 用户详情字段的共享代码也是如此。
  • userDetails: [ { userId: 'dref6977', firstName: 'red', lastName: 'nine', phoneNumber: '720066077', createdDate: 1638080948597 } ] 你在问这个吗?
  • **userdetails 字段没有替换值。状态对象有什么问题**这个,你能分享一下渲染逻辑的代码吗,我希望在那里休息一下。
  • 我刚刚发送了动作。 render() { return ( <div className="col-sm-8"> {this.chatSearch()} {this.chatWin()} {this.navBar()} </div> ); } 当我在输入字段(chatSearch() 函数)上输入 somthing 时,它将调用 send websocket 并接收 onmessage。这里将动作类型发送为 SEARCH 并更新 userDetails。
  • 哦,我明白了,如果你声明已经有 userDetails,那将被替换为 payload.userDetails。

标签: reactjs redux react-redux


【解决方案1】:

以下内容有效。重新启动我的 IDE 后。

import CHATS from '../actions/ChatMessages';
import MESSAGES from '../actions/Messages';
import SEARCH from '../actions/SearchUser';

const chatReducer = (state = {}, { type, payload }) => {
    switch (type) {
        case CHATS:
            payload.msgType = "";
            return { ...state, ...payload };
        case SEARCH:
        return { ...state, userDetails: [...payload.userDetails] };
        case MESSAGES:
            let index = state.chats.findIndex(
                chat => chat.chatId === payload.chatId
            );
            if (index !== -1) {
                let conn = state.chats[index];
                const msg = payload.msg;
                conn.lastMsg = msg;
                const messages = [...conn.messages, payload];
                const newState = { ...state };
                newState.chats[index] = { ...conn, messages };
                return newState;
            }
            return state;
        default:
            return state
    };
};

export default chatReducer;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2016-07-12
    • 2022-08-15
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多