React using react component inside of dangerouslySetInnerHTML

后端 未结 3 531
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 12:46

I have a question about using react. As you can see from the title, I\'m wondering if it is possible to use React component(that is created by React.createClass) inside of \

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 13:21

    dangerouslySetInnerHTML expects a JS object with __html property which should be valid HTML markup. Instead you are providing there and expecting it to render that component's html. React won't process there. dangerouslySetInnerHTML as name suggests should be avoided. Moreover what you are trying to accomplish here can easily be done through React only.

    const MySubComponent = React.createClass({
        render() {
            return (
  • MySubComponent {this.props.index}

  • ); } }); const MyComponent = React.createClass({ render() { let subComponentList = []; [1,2,3,4,5].forEach(function(i){ subComponentList.push(); }); return (
      {subComponentList}
    ); } }); ReactDOM.render(, mountNode);

提交回复
热议问题