1

I'm wondering how to dynamically request a component based on a variable value. What I'm trying to accomplish here is the following:

import Template1 from './Template1.jsx';
import Template2 from './Template2.jsx';

var ComponentTemplate = (some_condition === true) ? "Template1" : "Template2"

render() {
    <ComponentTemplate prop1="prop1 val" />
}

Is this even possible? If so, how?

5
  • What do the strings like "Template1" represent? Commented Jan 12, 2016 at 20:12
  • @JoshDavidMiller the actual names of a component. I updated the text to add some clarity Commented Jan 12, 2016 at 20:12
  • You mean like a string representation of a class name? Commented Jan 12, 2016 at 20:13
  • @JoshDavidMiller yes, exactly. Commented Jan 12, 2016 at 20:14
  • Could you just put the render itself within the conditional statement? Or just set the variable to the React template component itself? Commented Jan 12, 2016 at 20:15

3 Answers 3

3

It is not clear to me why you need to use a string representation of a class rather than just switch the component or use a conditional render:

var Component = some_condition === true ? Template1 : Template2;
// ...
return ( <Component /> );

But assuming this is an oversimplification, the easiest thing to do would be to use a mapping object that translates a string into a component. With ES2015 enhanced object literals, it becomes fairly straightforward:

var Components = {
  Template1,
  Template2,
};

var Component = condition ? Components['Template1'] : Components['Template2'];

// ...

return ( <Component /> );
Sign up to request clarification or add additional context in comments.

4 Comments

Nope, I think you nailed it the first time around.
It won't work I think. Because the React will compile the JSX tag by variable name. If you place Component to JSX. It will be React.createElement('Component') as well. It make no sense to using your original one.
@hlissnake I don't think I followed that, but it definitely will work. React.createElement takes either a string or a React class. It won't stringify a function/class. The call would actually be React.createElement( Component, {} );, where Component is a reference to a place in memory housing a function definition.
@Josh David Miller yes you are right. for primitive tag like 'span', 'div', it will compile into String. for element Class it will turn into Class object. Thanks
2

If you are just looking to render different component based on the condition you could actually have 2 other render function and inside render() you could check the condition and call corresponding render

render () {
  !this.state.isAuthorized? renderLogin(): renderTweets();
}

renderLogin () {
   <LoginView/>
}

renderTweet () {
   <ListTweets/>
}

Hope this is what you were looking for!

1 Comment

That's the correct way to define different React element
0

All the dynamic rendering should inside Render function. Because the JSX compile will depend on the Variable name not the Object reference.

If you write <Component />, it will transfer to React.createElement('Component').

Cannot refer to the dynamic component you want to choose.

depend on the condition when Render is running. Use different React tag you want.

render() {
    condition ? <Template1 /> : <Template2 />
}

Comments

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.