Why won't my nested React components render?

后端 未结 5 583
花落未央
花落未央 2020-12-15 17:44

I\'m having a problem with my React component. The nested children of my component ControlPanel don\'t seem to be rendering. Here is my code:

cl         


        
相关标签:
5条回答
  • 2020-12-15 18:32

    You can access the nested elements through props. So in your case do this:

    const ControlPanel = (props) => {
      return (
        <div className="control_panel" id="control_panel">
          {props.children}
        </div>
      );
    }
    
    0 讨论(0)
  • 2020-12-15 18:34

    To allow components to contain children and render them correctly, you have to use this.props.children. This is passed to all components with children as a prop and contains the children of the component, as explained by the React documentation:

    Containment

    Some components don't know their children ahead of time. This is especially common for components like Sidebar or Dialog that represent generic "boxes".

    We recommend that such components use the special children prop to pass children elements directly into their output:

    function FancyBorder(props) {
        return (
          <div className={'FancyBorder FancyBorder-' + props.color}>
            {props.children}
          </div>
        );
    }
    

    This lets other components pass arbitrary children to them by nesting the JSX

    function WelcomeDialog() {
        return (
          <FancyBorder color="blue">
            <h1 className="Dialog-title">
              Welcome
            </h1>
            <p className="Dialog-message">
              Thank you for visiting our spacecraft!
            </p>
          </FancyBorder>
        );
    }
    

    As described in the documentation, some components don't know their children ahead of time -- they may be generic wrappers or boxes of content that vary, which is what your ControlPanel is. So, to render the children of your component, you must render the children from the children prop explicitly in the parent's render method. Thus, apply it like this to your code:

    const ControlPanel = (props) => {
        return (
          <div className="control_panel" id="control_panel">
            {props.children}
          </div>
        );
    }
    

    Notice how props.children is rendered (not this.props.children because it is a function component).

    0 讨论(0)
  • 2020-12-15 18:36

    Your App.js (I understand that it is your JSX Index) should be:

    import React from 'react';
    import ReactDOM from 'react-dom';
    
    export default class App extends Component {
    render() {
    return (
      <div className="App">
        <ControlPanel>
          <CustomerDisplay />
        </ControlPanel>
      </div>
    );
    }
    }
    
    
    ReactDOM.render(<App/>, document.getElementById('YOUR_ROOT_ID'));
    

    Try to add export default before class (in all your components).

    0 讨论(0)
  • 2020-12-15 18:39

    You need to render the children in ControlPanel

    const ControlPanel = ({ children }) => {
        return (
          <div className="control_panel" id="control_panel">
            {children}
          </div>
        );
    }
    
    0 讨论(0)
  • 2020-12-15 18:45
    function FancyBorder(props) {
      return (
        <div className={'FancyBorder FancyBorder-' + props.color}>
          {props.children}
        </div>
      );
    }
    
    export default function WelcomeDialog() {
      return (
        <FancyBorder color="blue">
            <h1 className="Dialog-title">
               Welcome
            </h1>
            <p className="Dialog-message">
               Thank you for visiting our spacecraft!
            </p>
        </FancyBorder>
      );
    }
    
    

    Anything inside the<FancyBorder>JSX tag gets passed into the FancyBorder component as childrenprop. Since FancyBorder renders {props.children} inside a <div>, the passed elements appear in the final output.

    This is what I was looking after, check it out here https://reactjs.org/docs/composition-vs-inheritance.html

    0 讨论(0)
提交回复
热议问题