How to add Statefull component without constructor class in React?

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



filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
  
ReactDOM.render(<App />, document.querySelector('#root'))

chevron_right


Filename – App.js

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example 2: This example illustrates how to class property proposal to initialize state without constructor 

index.js:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
  
ReactDOM.render(<App />, document.querySelector('#root'))

chevron_right


App.js:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

react-js-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.