I know I can do condition with className but how about style?
<span style={{float:'right'}}>Something</span>
Like for above jsx, I want to float right if obj.name
is equal to 'active'
I know I can do condition with className but how about style?
<span style={{float:'right'}}>Something</span>
Like for above jsx, I want to float right if obj.name
is equal to 'active'
You may also want to set this in the render method of the component. I personally think that this is a more readable solution overall:
render() {
let customStyles = obj.name === 'active' ? { float: 'right' } : {};
return (<span style={customStyles}>Something</span>);
}
You'll need to have obj
in scope for this to work of course.
One Way, if you want the whole style
only when the object.name == 'active'
:
<span style={obj.name == 'active' ? {float:'right'} : {}}>Something</span>
Another, when you want to put the condition on separate style property
values:
<span style={{float: obj.name == 'active' ? 'right' : ''}}>Something</span>
You can follow the below mentioned Pattern.
reder(){
return(
let myStyle = {float:right; marginBottom:10;} //in JSX/JS hyphen sign is replaced with Camel case notation, it will automatically detected by ReactDOM renderer.
<span style={myStyle}></span>
);
}
for specifying any condition:
reder(){
return(
let myStyle = (obj.name == "active") ? {float:right; marginBottom:10;} : {};
<span style={myStyle}></span>
);
}