How to pass an Argument to a Method used inside JSX expression?
The expression in below example invokes the shape() and area() method, using the len and bre value as the argument.
Example
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.dimension = {
len: 40,
bre: 50
}
}
shape(l, b) {
return l == b ? "Sqaure" : "Rectangle";
}
area(l, b) {
return l * b;
}
render = () =>
<span class="d-block p-2 bg-dark text-white">
Area of { this.shape(this.dimension.len, this.dimension.bre) }:
{ this.area(this.dimension.len, this.dimension.bre) }
</span>
}
export default App;
Most Helpful This Week
How to use arrow function in React stateless components?
How to pass properties from parent component to child component in React JS?
Create React App
State and Props in React
Embed JavaScript Expressions in JSX
Create Functional Component using ES6 Arrow Syntax
React Elements
Component displays Height and Width on window resize
Display JSON Data in React JS
What is state in React?