I would like to ask you to use redux in react.

has been plagued by a problem, that is, the value saved by this redux,

.

for example, a data structure is obtained through api, but before the fetch comes down, the page will be rendered once. At this time, the data structure does not exist in redux, so there is a undefined problem

.

it is also said that an initial value should be given in redux. How should this initial value be given? I can"t make a copy of the server"s data structure and put it in redux as the initial value, can I? There are a lot of data structures that need to be obtained through the server, so don"t you have to copy them all?

then I try to create an identity in redux, that is, when the data structure is not yet available, when the data structure has been obtained for false, change it to the state of true, and then render render to check whether the logo is true or false, but there is another problem. There are many pages, so don"t all pages have to add such a check,

ask my friends for advice. Thank you very much for your advice.


the initial data structure writes
according to the data needed, for example,
my user information is stored in redux, and the initial value of the data

user: {
    id: '',
    name: '',
    roles: []
}

if it's a product list, that's it

productList: []

this means that you have the initial value, and undefined will not explode when you call user.id

.

ps: modify answer
you can also write
user: null because user is an object. You can use null to determine whether the user exists on the page. If the user does not exist, you do not need to render. In fact, you should consider what data the backend gives you and what data it doesn't give you, and then judge it according to if. In this case, if the superior does not exist, then the subordinate should not render

.

do not assign an initial value. Redux data should be as flat as possible. But I do. Stat.key = null. Use lodash to judge isPlainObject or isArray each time you use the data.

Menu