【问题标题】:Redux not passing state to containerRedux 没有将状态传递给容器
【发布时间】:2017-10-03 14:59:33
【问题描述】:

我在从减速器接收 ActiveMessage 对象的状态时遇到问题。当我单击消息列表上的一个对象时,我可以看到它通过正确的减速器流经动作,但没有到达 ActiveMessage 的本地道具。我不确定为什么它没有收到 active_message 道具上的状态。谁能指出我为什么这不起作用的正确方向?

message_list.js

import React, {Component} from 'react';
import {connect} from 'react-redux';
import MessageStub from "./message_stub";
import {activateMessage} from "../../actions/index";
import {bindActionCreators} from 'redux';

class MessageList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selected_message: null
        };

        this.selectMessage = this.selectMessage.bind(this);
    }

    selectMessage(event, index, message){
        this.setState({selected_message: index});
        this.props.activateMessage(message);

    }

    renderMessageList() {
        return this.props.message_list.map((message, index) =>
            <tr key={index}>
                <td className={index === this.state.selected_message ? "message-stub-body active-message-stub" : "message-stub-body"}
                    onClick={(e) => this.selectMessage(e, index, message)}>
                    <MessageStub />
                </td>
                <td>
                    {index === this.state.selected_message && <div className="message-stub-arrow"/>}
                </td>
            </tr>
        );
    }

    render() {
        return (
            <div className="message-list">
                <table className="message-list-table">
                    <tbody>
                    {this.renderMessageList()}
                    </tbody>
                </table>
            </div>
        );
    }
}

function mapStateToProps(state){
    return {
        message_list: state.message_list,
    };
}
function mapDispatchToProps(dispatch){
    return bindActionCreators({activateMessage: activateMessage}, dispatch)
}
export default connect(mapStateToProps, mapDispatchToProps)(MessageList);

active_message.js

import React, {Component} from 'react';
import {connect} from 'react-redux';

import MessageOut from "./message_out";
import MessageIn from "./message_in";

class ActiveMessage extends Component {
    constructor(props) {
        super(props);
        this.scrollToBottom = this.scrollToBottom.bind(this);

    }

    componentDidMount() {
        this.scrollToBottom();
    }

    componentDidUpdate() {
        this.scrollToBottom();
    }

    scrollToBottom() {
        this.el.scrollTop = this.el.scrollHeight;
    }

    renderMessages(){
        return this.props.active_message.messages.map((message, i) =>
            <MessageOut key={i} onClick={() => {
                this.scrollToBottom
            }}/>
        );

    }
    render() {
        console.log("rendering");
        console.log(this.props.active_message);
        if(!this.props.active_message){
            return <div>loading...</div>
        }
        return (
            <div className="active-message-div">
                <div className="active-message"
                     ref={el => {
                         this.el = el;
                     }}>
                    {this.renderMessages()}
                </div>
                <div className="message-reply-div">
                    <textarea className="message-reply-input" placeholder="Type a message..."/>
                </div>
            </div>
        );
    }
}

function mapStateToProps(state){
    console.log("mapping");
    return{
        active_message: state.active_message
    }
}

export default connect(mapStateToProps)(ActiveMessage);

actions/index.js

export const ACTIVATE_MESSAGE = 'activate_message';

export function activateMessage(message){
    console.log("calling action " + message)
    return{
        type: ACTIVATE_MESSAGE,
        payload: message
    };
}

reducers/index.js

import { combineReducers } from 'redux';
import MessageListReducer from './reducer_message_list';
import ActiveMessageReducer from './reducer_active_message';

const rootReducer = combineReducers({
    message_list: MessageListReducer,
    active_message: ActiveMessageReducer,
});

export default rootReducer;

reducer_active_message.js

import {ACTIVATE_MESSAGE} from "../actions/index";

export default function(state = null, action){
    switch(action.type){
        case ACTIVATE_MESSAGE:
            console.log("reducing through ACTIVE_MESSAGE");
            return action.payload;
    }
    return state;
}

reducer_message_list.js

export default function(){
    return[
        {
            user: {
                first_name: "First",
                last_name: "Last",
                type: "P"
            },
            messages: [
                {
                    message: "hi",
                    inbound: true
                },
                {
                    message: "bye",
                    inbound: false
                }
            ]


        },
        {
            user: {
                first_name: "First2",
                last_name: "Last2",
                type: "O"
            },
            messages: [
                {
                    message: "hi",
                    inbound: true
                },
                {
                    message: "bye",
                    inbound: false
                }
            ]


        }
    ];
}

【问题讨论】:

  • 您可以使用 redux 开发工具在您的商店中看到您的 active_message 吗?
  • 我是 React 新手,我不知道任何开发工具。我刚刚从 chrome 商店安装了 redux devtools,但它似乎对我来说根本不起作用。
  • 看起来你并没有改变减速器的任何状态,你只是返回了有效负载。你应该在你的 reducer 中使用 switch 在触发动作时返回一个新的 redux 状态,类似于 return state.set("active_message", action.payload);
  • @Dario 返回 action.payload 应该没问题,因为它将返回到名为 active_message 的减速器的根目录。没有?
  • @user1637466 Redux 开发工具必不可少!它为您的商店提供可视化界面。您需要将以下行添加到您的 Provider 商店。

标签: reactjs redux containers state


【解决方案1】:

我已将其加载到 CodeSandbox。我可以让它工作。

https://codesandbox.io/s/7y9nlvvprq

注意:您的 reducer_message_list 不是 reducer,只是一个静态数据集。

【讨论】:

  • 您是否更改了任何会影响状态的内容?
  • 我没有你的 组件,所以伪造它。我还在 中输出了用户详细信息,以显示正在传入状态。
  • 我没有改变状态
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 2020-10-29
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 2017-05-01
相关资源
最近更新 更多