GithubHelp home page GithubHelp logo

Comments (11)

sokra avatar sokra commented on April 28, 2024 9
constructor(props) {
  super(props);
  this.state = { foo: "bar" };
}

from react-starter.

AlastairTaft avatar AlastairTaft commented on April 28, 2024 4

Instead of doing it in the constructor this seems to work for me

class MyComponent extends Component {
  state = {
    foo: "bar"
  }
...

from react-starter.

barrystaes avatar barrystaes commented on April 28, 2024 2

Sorry, i forgot to set state in this example. (i updated my demo branch)

This works:

componentDidMount() {
  this.setState({foo: "works"});
}

My issue is that setting initial state "the normal way" no longer works:

getInitialState() {
  return {demo: "Hello #77"}
}

Did this change in React 0.13 perhaps? I could not find anything on that in API or specs docs.

(note: React.renderToString() does not call getInitialState() but i am not using prerender)

from react-starter.

EugeneZ avatar EugeneZ commented on April 28, 2024 1

@AlastairTaft That only works if you are using the proposed ECMAScript class fields syntax. For example by installing the relevant Babel plugin.

from react-starter.

sokra avatar sokra commented on April 28, 2024

this.state isn't used. null is the default value. Why is this an issue?

from react-starter.

barrystaes avatar barrystaes commented on April 28, 2024

😯 ah now i get this console warning.. (seems i missed this somehow)

Warning: getInitialState was defined on SomePage, a plain JavaScript class. This is only 
supported for classes created using React.createClass. Did you mean to define a stat 
property instead?

And indeed, i am not using React.createClass but

export default class SomePage extends React.Component {

And i was thinking this did the same thing, but apparently not.. insights welcome.

from react-starter.

barrystaes avatar barrystaes commented on April 28, 2024

Am i supposed to do this?

constructor(props) {
  this.setState({foo: "bar"});
}

from react-starter.

barrystaes avatar barrystaes commented on April 28, 2024

Thanks @sokra !
Seems there are more differences with regard to React.Component (React 0.13) than i realized.

If this is documented somewhere, i'd be happy to learn where. The only thing i found that mentioned such a thing was a coffeescript example.

from react-starter.

ibrahima avatar ibrahima commented on April 28, 2024

@barrystaes thanks for that link, wish that was documented more prominently somewhere. I think it's a reasonable change but I wasn't expecting it from reading the tutorials that mention ES6 class syntax.

from react-starter.

thebadasscoder avatar thebadasscoder commented on April 28, 2024

Make sure getInitialState is spelled correctly which was causing me to have that error. -__- Just take your time coding.

from react-starter.

sojelee avatar sojelee commented on April 28, 2024

@twilsonpierce awsome was getting same error, I mispelled getInitialState

from react-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.