1

I run into an issue in the rendering. I did some search online, but I didn't find a solution.

In the parent component:

<Child prop1={this.getHeader()}>

getHeader() : function () {
  return (<div> Header </div>)
}

In the Child component:

render() : function () {
  return (<div> {this.props.prop1} </div>)
}

However, the div didn't get rendered. I checked other solution and tried dangerouslySetInnerHTML, but it will render the string of " Header " rather than the element. Could i get some help?

1
  • Those code snippets are invalid ReactJs, also, your render function is returning another function instead of just JSX. Commented Apr 28, 2020 at 0:20

1 Answer 1

2

Here's a working solution:

class Parent extends React.Component {
  getHeader() {
    return <div> Header </div>;
  }

  render() {
    return <Child prop1={this.getHeader()} />;
  }
}

class Child extends React.Component {
  render() {
    return <div> {this.props.prop1} </div>;
  }
}

ReactDOM.render(<Parent />, document.querySelector('#app'));

https://jsfiddle.net/u5m29cw1/6/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. May I know why it works? I still don't understand what's the problem I have.
This makes no sense: getHeader() : function () {...}. I changed how you declare functions and that's pretty much it. Please, mark the answer as accept if you find it helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.