简体   繁体   English

在反应中集成动态数据

[英]integrate dynamic data in react

我创建了一个新的更好的问题在 react.js 中集成动态数据

The best practice is that feed data something like this最佳实践是像这样提供数据

[
   {
      data: [
        { name: 'Store Name', value: 'expreso polar'},
        { name: 'Store Id', value: 're485754re'}
      ]   
   }
]

name be the label and value be the data needs to shown in the table for corresponding label name为标签, value为对应标签需要在表格中显示的数据

This is something very common that I come across while coding and here is what I do.这是我在编码时遇到的非常常见的事情,这就是我所做的。 Im not sure how you are getting your json, whether it be by a fetch or if its hard coded but store it as a json object inside a variable我不确定您是如何获取 json 的,无论是通过 fetch 还是硬编码,但将其作为 json 对象存储在变量中

    const [metadata, setMetadata] = useState([])
    const data = response.json()
    setMetadata(data);
    const Table = useMemo(() => metadata.map(
        ({name, id, tags, address, logo}) => (
            <div>
                <div>Name: {name}</div>
                <div>Id: {id}</div>                
                <div>Tags: {tags}</div>
                <div>Address: {address}</div>
                <div>: {logo}</div>
            </div>
        )
        ),
        [metadata]);

Once you get your metadata in some sort of json format you can simply use the map function to map it all into jsx.一旦您以某种 json 格式获取元数据,您就可以简单地使用map函数将其全部映射到 jsx 中。 Then call it by using <Table/> .然后使用<Table/>调用它。

Using useMemo is always a good idea because it will update whenever one of the dependencies changes, in this case metadata.使用useMemo总是一个好主意,因为它会在依赖项之一发生变化时更新,在这种情况下是元数据。

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

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