Answers for "react initial state"

0

initialize state react

class App extends React.Component {
  constructor(props) {
    // Required step: always call the parent class' constructor
    super(props);

    // Set the state directly. Use props if necessary.
    this.state = {
      loggedIn: false,
      currentState: "not-panic",

      // Note: think carefully before initializing
      // state based on props!
      someInitialValue: this.props.initialValue
    }
  }

  render() {
    // whatever you like
  }
}
Posted by: Guest on April-25-2020
3

react initial state

//initial state
const [state, setState] = useState(0)
//change state
setState(1)
//change state with prev state value
setState((prevState) => {prevState + 2}) // 3
Posted by: Guest on September-09-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language