Generally, we set the initial state of the component inside the constructor class and change the state using the setState method. In React basically we write HTML looking code called JSX. JSX is not a valid JavaScript code but to make the developer’s life easier BABEL takes all the responsibility to convert JSX into valid JavaScript code and allow the developers to write code in HTML looking syntax. Similarly, the state can’t be initialized without constructor class, when we initialize state outside constructor class again Bable read the syntax and understands there is a need to create constructor inside the class and do all the required hard works behind the scene. This is called a class property proposal.
Syntax: Initialize state outside the constructor class using syntax.
state = {stateName1:stateValue1,
stateName2:stateName2,
.......
stateNamek:stateValuek}
Example 1: This example illustrates how to class property proposal to initialize state without constructor
index.js:
Javascript
import React from 'react'import ReactDOM from 'react-dom'import App from './App' ReactDOM.render(<App />, document.querySelector('#root')) |
Filename – App.js
Javascript
import React, { Component } from 'react' class App extends Component { // The class property proposal // The state initialization without // constructor class state = {msg : 'Hi, There!'} handleClick(){ // Changing state this.setState({msg : 'Welcome to the React world!'}) } render(){ return ( <div> <h2>Message :</h2> <p>{this.state.msg}</p> {/* Set click handler */} <button onClick={() => this.handleClick()}> Click here! </button> </div> ) } } export default App |
Output:

Example 2: This example illustrates how to class property proposal to initialize state without constructor
index.js:
Javascript
import React from 'react'import ReactDOM from 'react-dom'import App from './App' ReactDOM.render(<App />, document.querySelector('#root')) |
App.js:
Javascript
import React, { Component } from 'react' class App extends Component { static defaultProps = { courceContent : [ 'JSX', 'React Props', 'React State', 'React Lifecycle Methods', 'React Event Handlers', 'React Router', 'React Hooks', 'Readux', 'React Context' ] } // The class property proposal // The state initialization without // constructor class state = {msg : 'React Cource', content:''} renderContent(){ return ( <ul> {this.props.courceContent.map(content => ( <li>{content}</li> ))} </ul> ) } handleClick(){ //changing state this.setState({ msg : 'Cource Content', content : this.renderContent() }) } render(){ return ( <div> <h2>Message :</h2> <p>{this.state.msg}</p> <p>{this.state.content}</p> {/* set click handler */} <button onClick={() => this.handleClick()}> Click here to know contents! </button> </div> ) } } export default App |
Output:

Recommended Posts:
- How to communicate from parent component to the child component in Angular 9 ?
- How to change the state of react component on click?
- How to send state/props to another component in React with onClick?
- What is the difference between React Native and React?
- Differences between Functional Components and Class Components in React
- How to add class to an element without addClass() method in jQuery ?
- How to style the host element of the component in AngularJS?
- Design a form component which takes input from its user and displays a form
- How to pass string to a component using Angular2 ?
- How to enable routing and navigation between component pages in Angular 8 ?
- Difference between Directive and Component in AngularJS
- Adding Angular Material Component to Angular Application
- How to set default values for Angular 2 component properties?
- How to detect click event outside Angular component ?
- Introduction to React Native
- How React Native works?
- React.js (Introduction and Working)
- ReactJS | State in React
- Introduction to React-Redux
- Getting started with React Native? Read this first !
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


