score:13

Accepted answer

You don't need to do anything special. Just pass the title component as a prop, and then use {this.props.title} wherever you want it to be rendered:

class Panel extends React.Component {
  render() {
    return <div>
      {this.props.title}
      <div>Some other stuff...</div>
    </div>;
  }
}

class App extends React.Component {
  render() {
    var title = <Title>My Title</Title>;
    return <Panel title={title}/>;
  }
}

If you don't pass any value for the title prop (or if the value is false, null, or undefined) then nothing will be rendered there.

This is a fairly common pattern in React.

score:0

One thing you can do is have default props (usually initialised to a no-op) for your component.

For example, if you want to have an optional function prop:

class NewComponent extends React.Component {
    ...
    componentDidMount() {
        this.props.functionThatDoesSomething()
    }
}

NewComponent.defaultProps = {
    functionThatDoesSomething: () => {}
}

This way, parent components can choose to pass the function prop or not and your app won't crash due to the error

this.props.functionThatDoesSomething is not a function .

score:1

When you need an attribute from your optional prop, you will have to check first if the prop was delivered. Otherwise, you will get a:

TypeError: Cannot read property 'yourPropProperty' of undefined

In conditional rendering context (depending on my optional this.props.ignore array), this won't work:

{!this.props.ignore.includes('div')) && (
   <div>
      Hey
   </div>
)}

Instead, you should do:

{(!this.props.ignore || !this.props.ignore.includes('div'))) && (
   <div>
      Hey
   </div>
)}

score:4

you can do something like this

render(){
    <div>
        {this.props.title ? this.props.title : null}
        {this.props.children}
    </div>
}

basically if you pass a title element as a prop then create it as an element and render it. else just put in null...

to create it you would do something like this.

<Panel title={<Title>Something Here</Title>}
    <div> something here</div>
</Panel>

This is generally how react should handle optional child components


Related Query

More Query from same tag