1

I have a library of ReactJS components, as the following code:

components.js

class Comp1 extends Component {

    render () {
        return (
            <div>Component 1 Text: {this.props.text}</div>
        );
    }
}

class Comp2 extends Component {

    render () {
        return (
            <div>Component 2 Text: {this.props.text}</div>
        );
    }

}

export components = {
    Comp1,
    Comp2
}

The main component needs to choose which one to render based on a passed property:

main.js

import { components } from './components';
    
class Main extends Component {
    
        getComponent = (name) =>  {
            return components[name];
        };
        
        render () {
        
            let comp = this.getComponent(this.props.componentName);
            
            return (
                <div>
                    <comp   <=== HOW TO CALL THE GIVEN COMPONENT PASSING ITS PROPERTY
                        text={'This is component' + this.props.componentName }
                    />
                </div>
            );
        }
    }


class App extends Component {
        render () {
        
            return (
                <div>
                    <Main componentName='Comp1' /> // Or 'Comp2'
                </div>
            );
        }
    }
}

I need in the main code to render the component and pass its properties, but I can´t make it work (see the comments on code). A simple {comp} renders the component, but I need to be able to pass its properties accordingly.

What I´ve tried:

{comp text={'This is component' + this.props.componentName}}
<comp text={'This is component' + this.props.componentName}/>

None of them worked.

6
  • How did you render the App? What is the value of this.props.componentName? Commented Aug 26, 2017 at 0:35
  • @Dekel, this.props.componentName is Comp1 or Comp2. Updated the code with the initial render component. Commented Aug 26, 2017 at 1:09
  • @Mendes, Is export components working? And why just don't use ternary to check which component should be displayed? Commented Aug 26, 2017 at 1:12
  • @Andrew, export components works fine. This is a simple example to get the feature working. My library will have dozens of components.... Commented Aug 26, 2017 at 1:14
  • @Mendes, have you tried let Comp = this.getComponent(this.props.componentName); and <Comp text={'This is component' + this.props.componentName}/> ? Commented Aug 26, 2017 at 1:26

1 Answer 1

1

You component name need to begin with a UpperCase character. so it should look like

import { components } from './components';

class Main extends Component {

        getComponent = (name) =>  {
            return components[name];
        };

        render () {

            let Comp = this.getComponent(this.props.componentName);

            return (
                <div>
                    <Comp text={'This is component' + this.props.componentName }
                    />
                </div>
            );
        }
    }


class App extends Component {
        render () {

            return (
                <div>
                    <Main componentName='Comp1' /> // Or 'Comp2'
                </div>
            );
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.