initial state react code example
Example 1: usestate hook
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
Example 2: setstate react js
constructor(props) {
super(props);
this.state = {
isActive: true,
};
}
checkStatus = () => {
this.setState({
'isActive' : !this.state.isActive,
});
}
Example 3: initialize state react
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
loggedIn: false,
currentState: "not-panic",
someInitialValue: this.props.initialValue
}
}
render() {
}
}