@
@
@Richswitch2018-11-15 14:18:18
React
@Richswitch, 2018-11-15 14:18:18

How to render a component passed to props?

How to render a component from props? I know about HOC patterns and Render Props, but I haven't used either.
What do I need to do with this code to get my component to render?

// App.js
return (
<BlurBackground loading={loading} component={LoadingComponent}>
   <AnotherComponent />
</BlurBackground>
)
// BlurBackground.js
   const { loading, children, component } = this.props;
    return (
      <BlurWrapper loading={true}>
        {children}
        {component} // Вот этот должен рендериться
      </BlurWrapper>
    );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-11-15
_

// достаёте компонент из пропсов, обратите внимание, что
// имя переменной должно начинаться с большой буквы
const Component = this.props.component;

// ну и рендерите его
<Component />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question