Comments (11)
constructor(props) {
super(props);
this.state = { foo: "bar" };
}
from react-starter.
Instead of doing it in the constructor this seems to work for me
class MyComponent extends Component {
state = {
foo: "bar"
}
...
from react-starter.
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.
@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.
this.state
isn't used. null
is the default value. Why is this an issue?
from react-starter.
😯 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.
Am i supposed to do this?
constructor(props) {
this.setState({foo: "bar"});
}
from react-starter.
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.
@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.
Make sure getInitialState is spelled correctly which was causing me to have that error. -__- Just take your time coding.
from react-starter.
@twilsonpierce awsome was getting same error, I mispelled getInitialState
from react-starter.
Related Issues (20)
- Can't find container issue HOT 1
- What is best way to handle user authentication?
- Cannot find module 'tapable' HOT 8
- Does not work with current version of react-router 1.0.0 HOT 1
- Can's see any css style in page
- Failed Proptype warning
- [OUTDATED]? HOT 3
- npm start
- wwe
- Help me :( Can't start webpack-dev-server
- Get requested domain?
- Error defining includePaths for sass-loader HOT 1
- Request: a way to use the production React build with this HOT 2
- No easy way to include "global" CSS HOT 1
- How to exclude files from prerender? HOT 2
- Where to put <head> HOT 2
- dev server should proxy api instead of vice versa
- Plugin system?
- React Router injected `query` prop
- vagrant installation method issues HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-starter.