简体   繁体   English

将 state 设置为嵌套的 object 值

[英]React set state to nested object value

I need to set state on nested object value that changes dynamically Im not sure how this can be done, this is what Ive tried.我需要在动态变化的嵌套 object 值上设置 state 我不确定如何做到这一点,这是我尝试过的。

const [userRoles] = useState(null);

const { isLoading, user, error } = useAuth0();

useEffect(() => {
    console.log(user);
    // const i = Object.values(user).map(value => value.roles);
    // ^ this line gives me an react error boundary error 
  }, [user]);

// This is the provider
<UserProvider
      id="1"
      email={user?.email}
      roles={userRoles}
>

The user object looks like this:用户 object 如下所示:

{
    name: "GGG",
    "website.com": {
      roles: ["SuperUser"],
      details: {}
    },
    friends: {},
    otherData: {}
}

I need to grab the roles value but its parent, "website.com" changes everytime I call the api so i need to find a way to search for the roles.我需要获取角色值,但每次调用 api 时,它的父级“website.com”都会发生变化,因此我需要找到一种搜索角色的方法。

You can search the values for an element with key roles , and if found, return the roles value, otherwise undefined will be returned.您可以搜索具有键roles的元素的值,如果找到,则返回roles值,否则将返回undefined

Object.values(user).find(el => el.roles)?.roles;

Note: I totally agree with others that you should seek to normalize your data to not use any dynamically generated property keys.注意:我完全同意其他人的观点,即您应该寻求规范化您的数据以不使用任何动态生成的属性键。

 const user1 = { name: "GGG", "website.com": { roles: ["SuperUser"], details: {} }, friends: {}, otherData: {} } const user2 = { name: "GGG", friends: {}, otherData: {} } const roles1 = Object.values(user1).find(el => el.roles)?.roles; const roles2 = Object.values(user2).find(el => el.roles)?.roles; console.log(roles1); // ["SuperUser"] console.log(roles2); // undefined

I think you need to modify the shape of your object.我认为您需要修改 object 的形状。 I find it strange that some keys seem to be fixed, but one seems to be variable.我觉得奇怪的是,有些键似乎是固定的,但一个似乎是可变的。 Dynamic keys can be very useful, but this doesn't seem like the right place to use them.动态键可能非常有用,但这似乎不是使用它们的正确位置。 I suggest that you change the shape of the user object to something like this:我建议您将用户 object 的形状更改为如下所示:

{
    name: "GGG",
    site: {
      url: "website.com",
      roles: ["SuperUser"],
      details: {}
    },
    friends: {},
    otherData: {}
}

In your particular use case, fixed keys will save you lots and lots of headaches.在您的特定用例中,固定键将为您省去很多麻烦。

I would recommend what others have said about not having a dynamic key in your data object.我会推荐其他人所说的关于在您的数据 object 中没有动态密钥的说法。

For updating complex object states I know if you are using React Hooks you can use the spread operator and basically clone the state and update it with an updated version.为了更新复杂的 object 状态,我知道如果您使用 React Hooks,您可以使用扩展运算符并基本上克隆 state 并使用更新版本对其进行更新。 React hooks: How do I update state on a nested object with useState()? React hooks:如何使用 useState() 在嵌套的 object 上更新 state?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM