define function in class component react code example
Example 1: create-react-app class component
npx create-react-app my-app --scripts-version react-scripts@2.1.7
Example 2: class component params in react
<input
type= "text"
value={ this.state.value || "" }
placeholder="Enter Name"
/>
Example 3: function inside a class component react
export default class Archive extends React.Component {
saySomething(something) {
console.log(something);
}
handleClick(e) {
this.saySomething("element clicked");
}
componentDidMount() {
this.saySomething("component did mount");
}
render() {
return <button onClick={this.handleClick.bind(this)} value="Click me" />;
}
}
Example 4: react function component
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Example 5: react native function
class Foo extends Component {
handleClick() {
console.log('Click happened');
}
render() {
return <button onClick={this.handleClick.bind(this)}>Click Me</button>;
}
}