首页 文章

反应组件连接,redux状态改变......但没有更新组件?

提问于
浏览
5

我在使用 reduxreact-reduxredux-thunk 时,在状态更改后让某些React组件重新渲染时遇到问题 .

我相信我的减速器是非变异的,并且我通过 react-reduxconnect 订阅了 . 当我运行应用程序并查看浏览器控制台时,我看到组件的初始渲染(即初始状态为空状态),然后是状态更改(由 index.js 中的操作调度触发)....我希望使用新道具重新渲染的组件,但它不会发生 .

console log

我在这里张贴了一个回购:https://github.com/mattmoss/react-redux-no-update

node_modules 不在repo中,所以要运行,首先下载依赖项(运行 yarn 就足够了),然后 npm start .

一些摘录(请参阅回购中的完整资料):

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)
    );
}

1 回答

  • 4

    我不是100%,因为我自己还是比较新的 . 但是看看你的 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')
    );
    

相关问题