1

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.

1
  • 1
    Both codes are perfectly fine. There's nothing wrong with them, use whichever you find more readable or practical. Commented Nov 2, 2016 at 15:59

1 Answer 1

1

As far as I know it is a common practise. However, you should refactor your components in a way, that there is only one or none piece of logic in your render function.

Your parent component observes the loading state and renders the loader component or the "loaded" component.

If your state logic gets more advanced you then should take a look at the Flux architecture and its implementation library Redux.

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.