L
L
lexstile2021-05-27 00:42:37
React
lexstile, 2021-05-27 00:42:37

How to properly use useSelector and store.getState?

I saw a lot of opinions on this subject, including leads from our project.
I would like to understand in more detail when and what we use, and how it is better not to do it.

There are options:

// 1
const { name, age } = useSelector(state => state.userInfo);

// 2
const name = useSelector(state => state.userInfo.name);
const age = useSelector(state => state.userInfo.age);

// 3
const [name, age] = useSelector((state) => [
    state.userInfo.name,
    state.userInfo.age,
]);


Also, I want to understand when it is better to use store.getState() and is it worth it?

If, for example, the entire userInfo object is needed, is it better to get it through a hook or call store.getState()? (below examples)
const userInfo = useSelector(state => state.userInfo);
// or
const { userInfo } = store.getState();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Karo, 2021-05-27
@lexstile

The Redux Style Guide recommends the 2nd option
https://redux.js.org/style-guide/style-guide#call-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question