I want to wrap html tags around react state value when the value is not null, ie.
{this.state.keyword!=null?' '+'<span>'+this.state.keyword+'</span>'+' ':''}
but now, the page renders even the this.state.keyword is blank. How to do it? Thanks.
Use !this.state.keyword instead of this.state.keyword!=null
!this.state.keyword will check for all falsy values, like null, 0, empty string (not undefined though)
{!this.state.keyword ? '' : '<span>'+this.state.keyword+'</span>'+' '}
You can achieve this by :
{ this.state.keyword? ' '+'<span>'+this.state.keyword+'</span>'+' ':''}
Explanation : in your code you're checking for null only whereas you can check for all falsy conditions (eg null, undefined, empty string, 0 and false) by using above condition
null(?) Maybe it'sundefined.{ this.state.keyword ? <span>{this.state.keyword}</span> : null }which means, if this.state.keyword is not null or undefined, render the span, otherwise render null.