How to perform computation inside expression using ternary operator?
Expressions can be used for any computation, below example uses the ternary operator to determine whether length and breadth are equal or not. Based on that identify it is square or rectangle and also calculate area of that.
Example
import React, { Component } from 'react';
const length = 10, breadth = 20;
class App extends Component {
render = () =>
<span class="d-block p-2 bg-dark text-white">
Area of { length == breadth ? "Sqaure" : "Rectangle" }: { length * breadth}
</span>
}
export default App;
Most Helpful This Week
Getting Started with React and Hello World Example
How to pass data from child component to its parent in ReactJS?
How to pass properties from parent component to child component in React JS?
Component displays Height and Width on window resize
Using this keyword access component properties and methods
How to use function inside JSX expression?