Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Last active June 19, 2018 13:04
Show Gist options
  • Save nikkaroraa/5a3082c1f0712aa18724ff72f69920b0 to your computer and use it in GitHub Desktop.
Save nikkaroraa/5a3082c1f0712aa18724ff72f69920b0 to your computer and use it in GitHub Desktop.
Things to clear up before jumping into the world of React

5. Public Class Fields in JS

I have found a collection of blogs that will help you understand it. The links are as follows:

  1. Why is it important? (the use case)
  2. Public Class Fields in React
  3. Implementation example

Also, here is a simple explanation:

class User extends React.Component {
  state = {
    username: 'Tyler'
  } 
}

// rather than

class User extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      username: 'Tyler'
    }
  }
}
This is slightly different from Facebook's Setting the Initial State docs.

Having state outside the constructor() means it is a class field, which is a proposal for a new change to the language. It currently isn't supported by JavaScript, but thanks to Babel's fantastic powers of transpiling, we can use it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment