20
loading...
This website collects cookies to deliver better user experience
function Home(props){
return(
<div>
<p>This is the home page</p>
</div>
)}
class Home extends React.Component{
constructor(props){
super(props)
}
render(){
return <div>
<p>This is the home page</p>
</div>
}
}
useState
hook for managing state. The useState
hook will create and also change the state for the component.function Home(props){
const [count, setCount] = useState(0)
return(
<div>
<p>This is the home page</p>
<p> number of times clicked: {count}</p>
<button onClick={()=>setCount(count+1)}>Click me!</button>
</div>
}
this.state
and this.setState
. We create state my setting our state object equal to this.state
. We can then change our state by using this.setState
function and passing in our updated state object.class Home extends React.Component{
constructor(props){
super(props)
this.state={
count:0
}
render(){
return <div>
<p>This is the home page</p>
<p> number of times clicked: {this.state.count}</p>
<button onClick {()=>this.setState({count:this.state.count+1})}>Click me!</button>
</div>
}
}
this
keyword to access props. Since functional components are just regular functions, we can pass in props
as an argument to our function. This makes accessing them much simpler.<Home userName=‘Tripp’/>
function Home(props){
const [count, setCount] = useState(0)
return(
<div>
<h2>Hello {props.userName}!</h2>
<p>This is the home page</p>
<p> number of times clicked: {count}</p>
<button onClick={()=>setCount(count+1)}>Click me!</button>
</div>
}
this.props.userName
. We have to use the this
keyword since class components use ES6 class syntax.class Home extends React.Component{
constructor(props){
super(props)
this.state={
count:0
}
}
render(){
return <div>
<h2>Hello {props.userName}!</h2>
<p>This is the home page</p>
<p>number of times clicked: {this.state.count}</p>
<button onClick={()=>this.setState({count:this.state.count+1})}>Click me! </button>
</div>
}
}