0

The class receives the props.name which is the name from another Component, but it not renders properly.

class Component extends React.Component{
  constructor(props){
    super(props);
  }

  render(){
    var Element = this.props.name;
    return (  
        <Element />
    )
  }
}

const Dropdown = () =>{
    return(
    <div>
       <select>
          <option value="initial" selected>Select...</option>
          <option value="grapefruit">Grapefruit</option>
          <option value="lime">Lime</option>
          <option value="coconut">Coconut</option>
        </select>
    </div>
  )
}

class App extends React.Component {
  state = {
    components: []
  };

  render() {
    return (
      <div>
        <Component components={this.state.components} name="Dropdown" />          
      </div>
    );
  }
}

ReactDOM.render(<App />, mountNode);

The component renders fine when I write manually the name of the Component. For example:

render(){
  var Element = this.props.name;
  return (  
       <Dropdown />
  )
}
4
  • can you show how you are passing from parent component? Commented Jul 11, 2017 at 18:33
  • @MayankShukla done Commented Jul 11, 2017 at 18:46
  • instead of passing the string, try this: name={Dropdown} and use the same code. Commented Jul 11, 2017 at 18:48
  • 1
    @MayankShukla it was! If you wish post it to vote as solution. Commented Jul 11, 2017 at 18:56

4 Answers 4

1

How do you expect to generate Component out of string???

Do this:

class App extends React.Component {
  state = {
    components: []
  };

  render() {
    return (
      <div>
        <Component components={this.state.components} name={Dropdown} />          
      </div>
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass the props as tag argument. ex: . see the below example.

class parent extends React.Component{
constructor(){
super();
this.state={
   name:'ABC',
 }
}
render(){
   return(
  <Child argument={this.state.name} />
  )
 }
}

//stateless component don't have state
 function Child(props){
 return <div>{this.props.arguments}</div>
}

Comments

0

In the render() method you have to return one React element. That React element has to be a representation of DOM element. In your case you're trying to render a string, and that's the reason why isn't rendered (as you said) 'properly'. See documentation of render() method here.

I've updated your code so the name component is successfully rendered:

class Component extends React.Component{
  constructor(props){
    super(props);
  }

  render(){
    var Element = this.props.name;
    return (
    <div>
      {Element}
    </div>
    )
  }
}

const Dropdown = () =>{
    return(
    <div>
       <select>
          <option value="initial" selected>Select...</option>
          <option value="grapefruit">Grapefruit</option>
          <option value="lime">Lime</option>
          <option value="coconut">Coconut</option>
        </select>
    </div>
  )
}

class App extends React.Component {
  state = {
    components: []
  };

  render() {
    return (
      <div>
        <Component 
        components={this.state.components}       
        name={<div>Dropdown</div>}/>          
      </div>
    );
  }
}


React.render(<App/> , document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.min.js"></script>
<div id="app"></div>

Comments

0

Instead of passing the string from parent component, pass it like this:

<Component components={this.state.components} name={Dropdown} /> 

I think reason is:

<Element> will get converted into React.createElement(Element, {}) and which expect the first argument as a react component name, or a string (HTML tag), since you want to render a react component so don't pass the name as string.

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.