Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call a Function inside a Render in React/Jsx

I want to call a function inside some embedded html. I tried the following but the function isn't called. Would this be the incorrect way of calling a function inside a render method?

import React, { Component, PropTypes } from 'react';  export default class PatientTable extends Component {       constructor(props) {         super(props);         this.state = {           checking:false       };         this.renderIcon = this.renderIcon.bind(this);   }    renderIcon(){     console.log("came here")     return(       <div>Function called</div>     )   }    render() {     return (        <div className="patient-container">         {this.renderIcon}              </div>    );  } } 
like image 266
lost9123193 Avatar asked Oct 28 '16 04:10

lost9123193


People also ask

Can we call function in render?

In this article, we will learn how to call a function to render in ReactJS. React is having 2 types of components, i.e, Class based and Functional Components, but render method is only in Class based components. So we will make a class based component and call a function in that.

How do you call a function inside a React?

In React, the onClick handler allows you to call a function and perform an action when an element is clicked. onClick is the cornerstone of any React app. Click on any of the examples below to see code snippets and common uses: Call a Function After Clicking a Button.

How do you call a function after rendering?

The render() takes care of the DOM elements and is invoked every time the component is mounted or updated. Thus ,any JavaScript that needs to be executed after the render method has completed can go inside two methods: ComponentDidMount() in the mounting phase and ComponentDidUpdate() in the updating phase.


1 Answers

To call the function you have to add ()

{this.renderIcon()}    
like image 93
dknaack Avatar answered Oct 08 '22 02:10

dknaack