Is it possible to use if…else… statement in React render function?

Basically, I have a react component, its render() function body is as below: (It is my ideal one, which means it currently does not work)

render(){
    return (
        <div>
            <Element1/>
            <Element2/>

            // note: code does not work here
            if (this.props.hasImage) <ElementA/>
            else <ElementB/>

        </div>
    )
}

21 Answers
21

Leave a Comment