3

I'm trying to do nested conditionals to be rendered and one case would make me use .map()

renderClasses () {
        if (!this.state.classes.length) {
            console.log(this.state.userType)
            if (this.state.userType) return(<div>Add Class</div>)
            else return (<div>Join Class</div>)
        } else {
            return (<div>{
                this.state.classes.map((class) => {
                                             ^ unexpected token here 
                    <div>one class</div>
                })
            }</div>)
        }
    }

    render() {
        if (!this.state.isLogged) {
            return <Redirect to='/' />
        }
        return (
            <div>
                {
                    this.renderClasses()
                }
            </div>
        );
    }

Am i missing something? i tried wrapping everything into one <div> or maybe I understood it wrong? Thank you in advance

1

3 Answers 3

4

you do not return anything:

this.state.classes.map((item) => {
   <div>one class</div>
})

try to paste return statement

this.state.classes.map((item) => {
   return <div>one class</div>
})

But the error is cause by class being a reserved keyword, try to name it like item.

Sign up to request clarification or add additional context in comments.

3 Comments

This is right. It's worth noting that return is only required with the curley braces. So for example, both of these would return a div. .map(class => <div>One class</div>) and .map(class => { return <div>One class</div> })
i forgot about this, thank you for reminding about reserved keywords!
@adv I'm glad it works. If I helped please upvote or accept the answer :)
1

If you use {} in map function you need to use return as well. If its just a single statement, just ignore {}. You can use this -

this.state.classes.map(class => <div>one class</div>)

Comments

0

You should try returning from the map like this:

renderClasses () {
        if (!this.state.classes.length) {
            console.log(this.state.userType)
            if (this.state.userType) return(<div>Add Class</div>)
            else return (<div>Join Class</div>)
        } else {
            return (<div>{
                this.state.classes.map((class, index) => {
                    return (<div key={index}>one {class}</div>)
                })
            }</div>)
        }
    }

    render() {
        if (!this.state.isLogged) {
            return <Redirect to='/' />
        }
        return (
            <div>
                {
                    this.renderClasses()
                }
            </div>
        );
    }

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.