K
K
kova1ev2019-05-03 20:43:12
JavaScript
kova1ev, 2019-05-03 20:43:12

How to properly link react and redux?

Hello, here is the issue.
When using redax, I have such a moment. For example, there is a simple reducer:

const initialStateUser = {
    user: {}
};

function userReducer(state=initialStateUser, action) {
    return state;
}

const rootReducer = combineReducers({
    user: userReducer,
    ...
});

And in the component we bind:
const mapStateToProps = store => {
    return {
        user: store.user
    };
};

As a result, in order to get some property, for example, with the name name, from the user object, you have to write the following construction:
this.props.user.user.name
because we have an object from userReducer wrapped in another object in rootReducer. You can of course give different names, but something seems to me that I'm doing something wrong. Explain plz.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
profesor08, 2019-05-03
@kova1ev

const mapStateToProps = store => {
    return {
        user: store.user.user
    };
};

Or
const initialStateUser = {
    name: "Вася"
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question