admin管理员组文章数量:1426001
I am using redux for my react app. I am fetching user data from api, updating redux state with it and showing it into my ponent. The data is list of objects. The thing what causes me problem is setting redux initial state.
Reducer initial state:
const initState = {
users: []
}
Reducer action
case 'GET_USERS':
return {
users: action.users
}
});
Render in ponent
{this.props.users[1].name}
On first rendering i am getting error can't read from undefined property. That is cause on first rendering data isn't stored yet and that object with that property doesn't exist yet. I can solve that if i set initial state like:
const initState = {
users: [
{name: "", age: ""},
{name: "", age: ""}
]
}
In that case object with property would exist and i wouldn't get error. But i don't know how many objects i will have and i don't want to set initial state for every of them and theirs properties. So what is the way to properly set it?
I am using redux for my react app. I am fetching user data from api, updating redux state with it and showing it into my ponent. The data is list of objects. The thing what causes me problem is setting redux initial state.
Reducer initial state:
const initState = {
users: []
}
Reducer action
case 'GET_USERS':
return {
users: action.users
}
});
Render in ponent
{this.props.users[1].name}
On first rendering i am getting error can't read from undefined property. That is cause on first rendering data isn't stored yet and that object with that property doesn't exist yet. I can solve that if i set initial state like:
const initState = {
users: [
{name: "", age: ""},
{name: "", age: ""}
]
}
In that case object with property would exist and i wouldn't get error. But i don't know how many objects i will have and i don't want to set initial state for every of them and theirs properties. So what is the way to properly set it?
Share Improve this question asked Feb 1, 2019 at 12:54 bobbybobby 4553 gold badges9 silver badges27 bronze badges 2- You need iterate the array with a map, for example, and render the item inside the map. In this case, if the array is empty, no rendering would take place. – Ori Drori Commented Feb 1, 2019 at 12:56
- I think you should expand your render method to fully show us what you're attempting to output. – Adrian Lynch Commented Feb 1, 2019 at 12:57
4 Answers
Reset to default 1Just check if it is present and render conditionally
{this.props.users.length > 1 ? this.props.users[1].name : 'Not enough elements!'}
You shouldn't render a user by its position like that. Either you iterate all users or look into normalizing your data
<div>
{users.map(user) => (
<div>{users.name}</div>
)}
</div>
https://redux.js/recipes/structuring-reducers/normalizing-state-shape
I would use an empty array and check for the [].length property before rendering.
You mention your object structure
{name: "", age: ""}
and then you mention how to access users.name.firstname
. If you get an undefined
that means your dara hasn't loaded into your users array. So, instantiate with an empty array such as users:[]
and add the following:
const YourComponent = (props) => {
if (users.length === 0){
return <div> Loading </div>
}
return(
<div>
{users.map(user =>
<div>
{user.name}
</div>
)}
</div>
)
}
Your ponent will load before your array will be populated and will return the Loading div. When the array will be filled, the ponent will rerender and will map out your array.
本文标签: javascriptHow to set redux initial state for array of objectsStack Overflow
版权声明:本文标题:javascript - How to set redux initial state for array of objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470374a2659720.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论