In my react application, I have created a stateless Function Component which has a button in the JSX. I want to write a click handler for that button but defining the click handler outside the function throws me error. I am learning react and I have seen many tutorials that mentions we should try to create more stateless component hence I am following this approach.
Here is how I am doing- My component-
import React from 'react';
import ReactDOM from 'react-dom';
import {
Accordion,
AccordionItem,
AccordionItemTitle,
AccordionItemBody,
} from 'react-accessible-accordion';
import 'react-accessible-accordion/dist/fancy-example.css';
import 'react-accessible-accordion/dist/minimal-example.css';
handleClick = () =>{
alert("Button clicked");
}
const AccordionComp = (props) => (
<Accordion>
<AccordionItem>
<AccordionItemTitle>
<h4>List</h4>
</AccordionItemTitle>
<AccordionItemBody>
<table className="accordionTable">
<thead className="row">
<th className="col-sm-2">City Name</th>
<th className="col-sm-2">Name</th>
</thead>
<tbody>
<tr className="row">
<td className="col-sm-2">{props.propFromParent.city}</td>
<td className="col-sm-2">{props.propFromParent.name}</td>
<td className="col-sm-2"><button onClick={handleClick}>Click Me</button></td>
</tr>
</tbody>
</table>
</AccordionItemBody>
</AccordionItem>
</Accordion>
);
export default AccordionComp