端末を使用してディスパッチ・アクションをテストすると、Redux-loggerは自分の状態が正しく更新されていることを示します。しかし、私のコンポーネントは状態の変化の結果として再レンダリングされません。私はコンポーネントの再レンダリングに関するSOの答えを見てきましたが、回答の大部分は国家が突然変異していると主張しています。その結果、Reduxは再レンダリングしません。しかし、私はLodashのマージを使ってオブジェクトを深く掘り下げていますが、私は変更されたオブジェクトを返さないと確信しています。 (下の添付のスニペットを参照してください)Reduxストアの更新は反映されますが、コンポーネントは再レンダリングされません
あなたからのアドバイスを聞いて、私の髪を引き出したいですか?
const usersReducer = (state = {}, action) => {
Object.freeze(state); // avoid mutating state
console.log(state);
// returns an empty object
let newState = merge({}, state);
console.log(newState);
// returns my state with my dispatched action object inside already???
// newState for some reason already has new dispatched action
switch (action.type) {
case RECEIVE_USER:
let newUser = {[action.user.id] = action.user};
return merge(newUser, newUser);
case RECEIVE_USERS:
newState = {};
action.users.forEach(user => {
newState[user.id] = user;
});
return merge({}, newState);
default:
return state;
}
};
import { connect } from 'react-redux';
import { receiveUsers, receiveUser, refreshAll, requestUsers, requestUser } from '../../actions/user_actions';
import allUsers from '../../reducers/selectors';
import UserList from './user_list';
const mapStateToProps = (state) => ({
users: allUsers(state), // allUsers (selector that takes the state specfically the user Object and returns an array of user Objects)
state
});
const mapDispatchToProps = (dispatch) => ({
requestUser:() => dispatch(requestUser()),
requestUsers:() => dispatch(requestUsers()),
receiveUsers: (users) => dispatch(receiveUsers(users)),
receiveUser: (user) => dispatch(receiveUser(user)),
refreshAll: (users) => dispatch(refreshAll(users))
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(UserList);
コンテナコンポーネントを反応させる
表現的成分を反応させます
ストア
import { createStore, applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import RootReducer from '../reducers/root_reducer';
const logger = createLogger();
const configureStore = (preloadedState = {}) => {
return createStore(
RootReducer,
preloadedState,
applyMiddleware(logger));
};
// const configureStore = createStore(rootReducer, applyMiddleware(logger));
// oddly enough, when I have the store as a constant and not a function that returns the store constant, dispatching actions through the terminal will correctly update the state and rerender the component
export default configureStore;
はセレクタ
const allUsers = ({ users }) => {
return Object.keys(users).map(id => (
users[id]
));
};
export default allUsers;
コンポーネントコードを追加できますか?コンポーネントをストアに配線する方法には問題があります。 – Mustafa
これはあなたの問題に関連していないかもしれませんが、あなたのマージの使い方は私にとっては意味がありません。あなたは正確に何を達成することを期待していますか?私はあなただけのスプラットを使用することをお勧めします。 'return {... state、x:42}' –
ストアに 'usersReducer'をどこに追加しますか?ストアの状態が変更されたときにコンポーネントの 'mapStateToProps'が呼び出されていることを確認できますか?そうでない場合は、減速機が適切に実行されていない可能性があります。 – Mustafa