kukab
kukab

Reputation: 571

Unexpected keyword 'this'

i have created a component if i write {this.state.tags.length === 0 ? "hello" : <ul>{this.gettag()}</ul> } i am getting no error if I write like this {this.state.tags.length === 0 ? "hello" : {this.gettag()} } with out ul tag I am getting error here is my code

class Count extends Component {
   state={
       tags:['kukab', 'saqib' , '40'],
   };
    render() {
        return (
            <div>
                {this.state.tags.length === 0 ? "hello" : <ul>{this.gettag()}</ul> }
                
            </div>
            
        );
    }
    gettag(){
    return(
        <div>
           
        <ul>{this.state.tags.map(tag => <li key={tag}>{tag}</li> )}</ul>

        </div>
    )

    }
}

enter image description here

Upvotes: 3

Views: 978

Answers (1)

Lanil Marasinghe
Lanil Marasinghe

Reputation: 2915

If you want to call this function without <ul> tag, Use {this.state.tags.length === 0 ? "hello" : this.gettag()}. Just remove two curly braces surrounding this.gettag(). But if you want to use it inside <ul> tag, Use {this.state.tags.length === 0 ? "hello" : <ul>{this.gettag()}</ul>}

Upvotes: 3

Related Questions