how to handle props in functional component code example
Example 1: function component with props
import React from "react"
function Checkbox(props){
return (
<div>
<input type="checkbox" />
<label>{props.value}</label>
</div>
)
}
export default Checkbox
Example 2: how to use props in functional component in react
import React, { useState } from 'react';
import './App.css';
import Todo from './components/Todo'
function App() {
const [todos, setTodos] = useState([
{
id: 1,
title: 'This is first list'
},
{
id: 2,
title: 'This is second list'
},
{
id: 3,
title: 'This is third list'
},
]);
return (
<div className="App">
<h1></h1>
<Todo todos={todos}/>
</div>
);
}
export default App;
Example 3: react native function
class Foo extends Component {
handleClick() {
console.log('Click happened');
}
render() {
return <button onClick={this.handleClick.bind(this)}>Click Me</button>;
}
}
Example 4: class to functional component react
class MyComponent extends React.Component {
state: {
name: 'Bob'
}
render() {
return (
<p>Hello, my name is {this.state.name}</p>
);
}
}
const MyComponent = () => {
const [name, setName] = React.useState('Bob');
return (<p>Hello, my name is {name}</p>);
}