代码之家  ›  专栏  ›  技术社区  ›  Edwin Babu

道具到桌子细节

  •  0
  • Edwin Babu  · 技术社区  · 8 年前

    我有一个fomat的json

    {
        description : "Meeting Description"
        name : "Meeting name"
        owner : {
            name: "Creator Name",
            email: "Creator Name"
        }
    }
    

    我需要使用表以这种格式显示详细信息

    Meeting Name : Meeting name
    Description  : Meeting name
    Creator Name : Creator Name
    Creator Email: Creator Name
    

    这是我用过的代码。我使用了spread操作符从内部json获取细节。

    function DisplayDetails(props) {
        return (
            <table style={{ padding: "10px" }}>
                <tbody>
                    <tr>
                        <td>Meeting Name</td>
                        <th>:&nbsp;{props.name}</th>
                    </tr>
                    <tr>
                        <td>Description</td>
                        <th>:&nbsp;{props.description}</th>
                    </tr>
                    <DisplayOwner {...props.owner} />
    
                </tbody>
            </table>
        )
    }
    
    function DisplayOwner(props) {
        return (
            <div>
                <tr>
                    <td>Creator Name:</td>
                    <td>{props.name}</td>
                </tr>
                <tr>
                    <td>Creator Email:</td>
                    <td>{props.email}</td>
                </tr>
            </div>
        )
    }
    

    此代码显示警告

    validateDOMNesting(...): <tr> cannot appear as a child of <div>.
    

    2 回复  |  直到 8 年前
        1
  •  2
  •   Matt Carlotta    8 年前

    工作示例: https://codesandbox.io/s/4qjq5z7w87

    索引.js

    import React from "react";
    import ReactDOM from "react-dom";
    import DisplayDetails from "./displayDetails";
    import "./styles.css";
    
    const data = {
      description: "Meeting Description",
      name: "Meeting Name",
      owner: {
        name: "Creator Name",
        email: "Creator Email"
      }
    };
    
    const App = () => (
      <div className="App">
        <h1>Display Details</h1>
        <DisplayDetails {...data} />
      </div>
    );
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    显示详细信息.js

    import React from "react";
    import DisplayOwner from "./displayOwner";
    
    export default ({ description, name, owner }) => (
      <table style={{ padding: "10px" }}>
        <tbody>
          <tr>
            <td>Meeting Name:</td>
            <th>{name}</th>
          </tr>
          <tr>
            <td>Description:</td>
            <th>{description}</th>
          </tr>
          <DisplayOwner {...owner} />
        </tbody>
      </table>
    );
    

    显示所有者.js

    import React, { Fragment } from "react";
    
    export default ({ email, name }) => (
      <Fragment>
        <tr>
          <td>Creator Name:</td>
          <td>{name}</td>
        </tr>
        <tr>
          <td>Creator Email:</td>
          <td>{email}</td>
        </tr>
      </Fragment>
    );
    
        2
  •  1
  •   Ayushya    8 年前

    更新如下,使用片段, https://reactjs.org/docs/fragments.html#short-syntax https://reactjs.org/docs/fragments.html

    import React from 'react';
    
    ...
    
    function DisplayOwner(props) {
        return (
            <React.Fragment>
                <tr>
                    <td>Creator Name:</td>
                    <td>{props.name}</td>
                </tr>
                <tr>
                    <td>Creator Email:</td>
                    <td>{props.email}</td>
                </tr>
            </React.Fragment>
        )
    }