I'm trying to write ReactJS component that will be showing 2 view types toggled by loaded flag in the state. Im asking about a loader spinner but consider that this issue can be more complicated for showing 2 different DOM trees based on that flag. I would really like to make my code as clearer as I can.
What I have in mind is 2 ways to write it:
First way is very intuitive for me but not readable + I have to add another wrapping div inside the else statement:
render() {
return (
<div>
{!this.state.loaded ? <Loader /> : (
<div>
<Arrow direction="left" onClick={this.slideLeft}/>
<ul>
{
this.props.images.map((image, index) => {
return (
<li key={index} styleName={index === this.state.active ? 'active' : ''}>
<ImageItem src={image} />
</li>
)
})
}
</ul>
<Arrow direction="right" onClick={this.slideRight}/>
</div>
)}
</div>
);
}
The second way is to return before rendering any of the loaded code, is it a good practice to keep logic inside the render function like this? + "content" class div is duplicated:
render() {
if(!this.state.loaded){
return <div className="content"><Loader /></div>;
}
return (
<div className="content">
<Arrow direction="left" onClick={this.slideLeft}/>
<ul>
{
this.props.images.map((image, index) => {
return (
<li key={index} styleName={index === this.state.active ? 'active' : ''}>
<ImageItem src={image} />
</li>
)
})
}
</ul>
<Arrow direction="right" onClick={this.slideRight}/>
</div>
);
}
maybe you have other suggestions for me? thanks.