简体   繁体   English

react 组件已连接,redux 状态发生变化……但组件没有更新?

[英]react component connected, redux state changes… but no update to component?

I'm creating a React/Redux front-end for a multi-channel chat app.我正在为多渠道聊天应用程序创建 React/Redux 前端。 I'm having problems getting some React components to re-render after state change while using redux , react-redux , and redux-thunk .我在使用reduxreact-reduxredux-thunk在状态更改后重新渲染一些 React 组件时遇到问题。

I believe that my reducers are non-mutating, and that I'm subscribed via react-redux 's connect .我相信我的减速器是非变异的,并且我是通过react-reduxconnect订阅的。 When I run the app and view the browser console, I see the initial render of the component (ie with initial, empty state), then the state change (triggered by an action dispatch in index.js ).... I would then expect the component to re-render with new props, but it doesn't happen.当我运行应用程序并查看浏览器控制台时,我看到组件的初始渲染(即初始状态为空),然后状态发生变化(由index.js的动作调度触发)......然后我会期望组件使用新的道具重新渲染,但它不会发生。

控制台日志

I've put up a repo here: https://github.com/mattmoss/react-redux-no-update我在这里放了一个仓库: https : //github.com/mattmoss/react-redux-no-update

node_modules is not in the repo, so to run, first download dependencies (running yarn is sufficient), then npm start . node_modules不在 repo 中,所以要运行,首先下载依赖项(运行yarn就足够了),然后npm start

Some excerpts (see full source in repo):一些摘录(请参阅 repo 中的完整源代码):

reducers/channelList.js

import * as c from '../actions/constants';

export default function channelList(state = [], action) {
    switch (action.type) {
        case c.FETCH_CHANNELS_SUCCESS:
            return action.channels;
        default:
            return state;
    }
}

actions/channelActions.js

export function fetchChannels() {
    return (dispatch) => {
        return ChannelApi.allChannels()
            .then(channels => dispatch(fetchChannelsSuccess(channels)))
            .catch(error => { throw(error); });
    };
}

export function fetchChannelsSuccess(channels) {
    return {
        type: c.FETCH_CHANNELS_SUCCESS,
        channels
    };
}

components/ChannelListView.js

class ChannelListView extends React.Component {
    render() {
        const { channels, current, onSelect } = this.props;

        console.log("channels:", channels, "current:", current);

        return (
            <ListGroup>
                {channels.map(channel =>
                    <ListGroupItem
                        key={channel.id}
                        active={channel.id === this.props.current}
                        onClick={onSelect(channel.id)}
                    >
                        <strong>#{channel.name}</strong>
                    </ListGroupItem>
                )}
            </ListGroup>
        );
    }
}

export default ChannelListView;

containers/ChannelList.js

import ChannelListView from '../components/ChannelListView';

const mapStateToProps = (state, ownProps) => {
    return {
        channels: state.channelList,
        current: state.currentChannel
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        onSelect: (id) => () => {}
    };
};

export default connect(mapStateToProps, mapDispatchToProps)(ChannelListView);

App.js

class App extends Component {
  render() {
    return (
      <Grid>
        <Row>
          <Col>
            <h1>Channels</h1>
            <ChannelList />
          </Col>
        </Row>
      </Grid>
    );
  }
}

index.js

const store = configureStore();
store.dispatch(fetchChannels());

ReactDOM.render(
    <Provider store={configureStore()}>
        <App />
    </Provider>,
    document.getElementById('root')
);

store/configureStore.js

import { createStore, applyMiddleware } from 'redux';
import rootReducer from '../reducers/rootReducer';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

export default function configureStore() {
    return createStore(
        rootReducer,
        applyMiddleware(thunk, logger)
    );
}

I'm not 100% as I am still relatively new to React myself.我不是 100%,因为我自己对 React 还是比较陌生。 But look at your index.js script.但是看看你的index.js脚本。

// You configure the store, then dispatch the fetchChannels action
const store = configureStore();
store.dispatch(fetchChannels());

ReactDOM.render(
    // But here, you're recreating the store again, which I think will re-initialise an empty store
    // Change this to use the `store` variable from above.
    <Provider store={configureStore()}>
        <App />
    </Provider>,
    document.getElementById('root')
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM