首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React中的嵌套对象和表中的显示

React中的嵌套对象和表中的显示
EN

Stack Overflow用户
提问于 2022-10-06 13:24:50
回答 1查看 66关注 0票数 1

我在遍历我的对象时遇到了困难。

代码语言:javascript
复制
 export default function UserProfile(props) {
// props.user has Object{social:Object{email:{},.....}}
    const form = useForm({ // Mantine form
        initialValues: {
         social: {
            email: {
              email: props.user.email,
              notifications: true,
            },
            facebook: {
              fb_id: props.user.fb,
              notifications: true,
            },
            telegram: {
              chat_id: props.user.tele,
              notifications: false,
            },
          },
        },
    
    return(   
               <Tabledata
                  social={{ ...form.getInputProps("social") }}
                  session={session}
                />
          )
     }

以下是我的表数据组件:

代码语言:javascript
复制
function Tabledata({ social }) {
  console.log(social);  // in Console Object { value: {…}, onChange: getInputOnChange(val) }
  

  return (
    <>
      <Table>
        <thead>
          <tr>
            <th>Communication channel</th>
            <th>Notifications</th>
          </tr>
        </thead>
        <tbody>
          {Object.keys(social.value).map((data, index) => (
            //console.log(data)
            
             <tr key={index}>
               <td>{data}</td>
               <td>{data.notifications ?
            (<CheckCircleIcon
            className="h-6 w-6 text-green-500" />) : 
           (<XCircleIcon className="h-6 w-6 text-   red-500"/>)}</td>
             </tr>
          ))}
        </tbody>
      </Table>
    </>
  );
}

我应该像下面这样拥有这个表,但无法实现它:

我尝试过不同的方法来迭代,但是没有得到想要的结果。欢迎任何建议。

EN

回答 1

Stack Overflow用户

发布于 2022-10-06 20:41:45

我能在别人的帮助下解决这个问题

下面是我对表数据的解决方案:

代码语言:javascript
复制
function Tabledata({ social }) {   
  return (
    <>
      <Table>
        <thead>
          <tr>
            <th>Communication channel</th>
            <th>Notifications</th>
          </tr>
        </thead>
        <tbody>
          {Object.keys(social.value).map((data, index) => (               
             <tr key={index}>
               <td>{data}</td>
               <td>{social.value[data]['notifications'] ? // The way I can access the value of the notification
            (<CheckCircleIcon
            className="h-6 w-6 text-green-500" />) : 
           (<XCircleIcon className="h-6 w-6 text-   red-500"/>)}</td>
             </tr>
          ))}
        </tbody>
      </Table>
    </>
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73974621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档