Mat*_*oss 6 javascript reactjs redux react-redux react-thunk
我正在为多频道聊天应用创建一个React/Redux前端.我有得到一些问题,反应的组分,同时使用状态改变后重新渲染redux,react-redux和redux-thunk.
我相信我的减速器是非变异的,而且我是通过它订阅react-redux的connect.当我运行应用程序并查看浏览器控制台时,我看到组件的初始渲染(即具有初始,空状态),然后状态更改(由操作调度触发index.js)....我会期望组件使用新道具重新渲染,但它不会发生.
我在这里张贴了一个回购:https: //github.com/mattmoss/react-redux-no-update
node_modules不在repo中,所以要运行,首先下载依赖项(运行yarn就足够了),然后npm start.
一些摘录(请参阅回购中的完整资料):
reducers/channelList.jsimport * 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;
}
}
Run Code Online (Sandbox Code Playgroud)
actions/channelActions.jsexport 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
};
}
Run Code Online (Sandbox Code Playgroud)
components/ChannelListView.jsclass 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;
Run Code Online (Sandbox Code Playgroud)
containers/ChannelList.jsimport 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);
Run Code Online (Sandbox Code Playgroud)
App.jsclass App extends Component {
render() {
return (
<Grid>
<Row>
<Col>
<h1>Channels</h1>
<ChannelList />
</Col>
</Row>
</Grid>
);
}
}
Run Code Online (Sandbox Code Playgroud)
index.jsconst store = configureStore();
store.dispatch(fetchChannels());
ReactDOM.render(
<Provider store={configureStore()}>
<App />
</Provider>,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
store/configureStore.jsimport { 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)
);
}
Run Code Online (Sandbox Code Playgroud)
我不是 100%,因为我自己对 React 还是比较陌生。但是看看你的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')
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
431 次 |
| 最近记录: |