0

I've created a custom component and I want to add child element when the component renders if a certain property is set to be true. I used the following code, but the component is not rendered. what am I doing wrong here.

    let deleteNode = '';
    if(deletable){
        deleteNode = '<div />'
    }

    let defaultClasses = 'chips chips-rounded';

    return (
        <div className={classNames(classes, defaultClasses)} onClick={ this.onClick }>
            {avatar}
            <span>{this.props.labelText}</span>
            {deleteNode}
        </div>
    )
2
  • 1
    you can use ternary {deletable ? '<div />' : ''} Commented Nov 24, 2017 at 9:17
  • Well your is much cleaner! :) Commented Nov 24, 2017 at 9:30

2 Answers 2

1

You are trying to render a component but actually you are just sending string in your deleteNode. Your code should be something like below

if(deletable){
    deleteNode = (<div />);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I different approach would be:

render() {
  const defaultClasses = 'chips chips-rounded'
  return (
    <div className={classNames(classes, defaultClasses)} onClick={ this.onClick }>
        {avatar}
        <span>{this.props.labelText}</span>
        {deletable && <div />}
    </div>
  )
}

So you don't need the extra if checking.

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.