Adding ClassNames To Odd And Even Elements In ReactJs
I am trying to add the className 'direction-r' to the divs in the 'timeline' list that are even and className 'direction-l' to every other item (odd). I know how it can be achieved
Solution 1:
Instead of modifying the DOM, React allows you to define rendering logic inside the render method before the DOM is created:
var listItems
for( var i=0; i<4; i++) {
listItems.push(
<li key={i}>
<div className={'direction-'+(i%2 ? 'r':'l')}>
<div className="title-wrapper">
<span className="title professional">
Declared bankrupcy
</span>
<span className="time-wrapper"><span class="time">2015 - present</span></span>
</div>
<div className="detail">
<p className="place">Corporation</p>
</div>
</div>
</li>
)
}
return (
<div className="timeline-container">
<h3>Timeline</h3>
<ul className="timeline">
{listItems}
</ul>
</div>
)
Solution 2:
This should work.
class MainTimeline extends React.Component {
render() {
var isEven = (your even odd logic);
var directionClass = isEven ? 'direction-r' : 'direction-l';
return (
<div className="timeline-container">
<h3>Timeline</h3>
<ul className="timeline">
<li>
<div className={directionClass}>
<div className="title-wrapper">
<span className="title professional">
Declared bankrupcy
</span>
<span className="time-wrapper"><span class="time">2015 - present</span></span>
</div>
<div className="detail">
<p className="place">Corporation</p>
</div>
</div>
</li>
</ul>
</div>
);
}
}
Post a Comment for "Adding ClassNames To Odd And Even Elements In ReactJs"