我正在用React-Redux一起做我的第一步。我在国家管理方面遇到了麻烦。
一切都在按预期进行。我有一个用来触发动作分派器(FetchUserAction)的onClick按钮。此操作执行HTTP请求。Reducer使用数据并修改存储。我正在调试,看到connect函数上的状态(带有数据)运行良好。
我的理解是,当修改状态时,组件将调用render方法。这是正在发生的!真正的问题是,当我通过prop将新数据传递给子组件时,我会看到它,但这并没有发生。我做错了什么?
class Home extends Component {
    constructor(props) {
        super(props);
        this.state = {
            users: []
        };
        this.onButtonUserClick = this.onButtonUserClick.bind(this);
    }
    onButtonUserClick(){
        this.props.fetchUser();
    }
    render() {
        return (
                <div className={styles.home}>
                    <FetchUsersButton
                        fetchUsers={this.onButtonUserClick}/>
                    <UsersTable
                        users={this.props.users}/>
                </div>
        );
    }
}
function mapDispatchToProps(dispatch) {
    return bindActionCreators({ fetchUser }, dispatch);
}
const mapStateToProps = (state) => {
    return {
        users: state.users
    };
};
export default connect(mapStateToProps, mapDispatchToProps)(Home);
import React, {Component} from 'react';
import {Table, Thead, TBody, Td, Th, Tr} from 'reactable';
import styles from './index.css';
export default class UsersTable extends Component{
    constructor(props){
            super(props);
            this.state = {
                users: props.users
            };
            this.renderUsersTable = this.renderUsersTable.bind(this);
    }
    renderUsersTable() {
        return this.state.users.map(user => {
            return (
                <Tr key={user}>
                    <Td column="steamId">
                        {user.steam_id_64}
                    </Td>
                    <Td column="invited">
                        Yes
                    </Td>
                </Tr>
            );
        });
    }
    render(){
        return(
            <Table className={`table ${styles.usersTable}`}>
                    <Thead>
                    <Th column="steamId">
                            <strong className="name-header">SteamID</strong>
                    </Th>
                    <Th column="invited">
                            <strong>Invited</strong>
                    </Th>
                    </Thead>
                    { this.state.users.length > 0 ? this.renderUsersTable() : null}
            </Table>
        );
    }
}
import { FETCH_USERS } from "../actions/index";
export default function(state = [], action) {
    switch (action.type) {
        case FETCH_USERS:
            return [action.payload.data, ...state];
    }
    return state;
}
发布于 2017-07-24 12:28:05
您的<UserList />呈现正在查看它的内部状态,以确定要呈现的内容。您将需要它来查看道具以使其正常工作。
renderUsersTable() {
        // change this.state to this.props
        return this.state.users.map(user => {
            return (
                <Tr key={user}>
                    <Td column="steamId">
                        {user.steam_id_64}
                    </Td>
                    <Td column="invited">
                        Yes
                    </Td>
                </Tr>
            );
        });
    }将此行修改为引用props以及{ this.state.users.length > 0 ? this.renderUsersTable() : null}
附注:您可以将<UserList />转换为stateless functional component,并摆脱类开销。
https://stackoverflow.com/questions/45272584
复制相似问题