How to use function inside JSX expression?


A function can be defined and use by the expression, in that case the output of a function is taken by the component to produce content.

Example

import React, { Component } from 'react';
 
const length = 20, breadth = 10;
 
function shape() {
    return length == breadth ? "Sqaure" : "Rectangle";
}
 
function area() {
    return length * breadth;
}
 
class App extends Component {
  render = () => 
  <span class="d-block p-2 bg-dark text-white">
    Area of { shape() }: { area() }
  </span>
}
 
export default App;
Most Helpful This Week