A React.js case study
blog.krawaller.se
blog.krawaller.se
In short:
- Don't pass around components and call methods on them; prefer to pass props instead
- Conditional rendering instead of hiding, exactly as [3]
- Use the JSX harmony transforms
- Declare props
- Use classSet
- setState only needs to include props that change, rest stay the same
[1] https://github.com/ianobermiller/reactexperiment/commits/
TLDR: JSX transformer makes the following ES6 features compatible with old browsers (IE8+) * arrow functions [http://tc39wiki.calculist.org/es6/arrow-functions/] * classes [https://github.com/esnext/es6-class] * destructuring [http://fitzgeraldnick.com/weblog/50/] * object concise methods [http://ariya.ofilabs.com/2013/03/es6-and-method-definitions....] * rest parameters [http://tc39wiki.calculist.org/es6/rest-parameters/] * template strings [http://tc39wiki.calculist.org/es6/template-strings/] * object literal property shorthand [http://tc39wiki.calculist.org/es6/object-literal-enhancement...]
Notably absent: default parameter values and block scoping ("let")
I suspect that you were doing something wrong that triggered this error. Unmounting components based on state is perfectly idiomatic React, and I do it all the time:
{this.state.mode === 'map' && <Map/>}
You pay for the added DOM work, of course, but this is usually negligible in my experience, especially in your case. You do have to make sure you follow the component lifecycle so that you don't try to use getDOMNode() or similar with a component that is not currently mounted.http://facebook.github.io/react/docs/jsx-in-depth.html
Something like <Map foo={bar}/> would compile to a function call Map({foo: bar}). Behind the scenes, React is not dealing with HTML.
You can use React without JSX, but it's useful.
Sometimes that means using non-idiomatic constructs or just having some rough edges. Please be considerate.
var classes = _.reduce(["flipped","correct","wrong"],function(m,c){return m+(this.state[c]?c+" ":"");},"",this);
seem very inelegant compared to say, Angular's ngClass, which does the same thing in a data-driven way. But, that's not to say Angular is free from template logic either.http://facebook.github.io/react/docs/class-name-manipulation...
I think the React authors agree that React classes shouldn't contain excessive logic; if your class does, consider extracting it to somewhere more appropriate.
Thanx for the pointers!
http://facebook.github.io/react/docs/class-name-manipulation...
className={this.state.playing ? "hidden" : "showing"}
instead of <div>
{(this.state.playing
? <Board/>
: <Wordform/> )}
</div>Although as pointed out in the Disqus comment, choosingg what to render is also fine, I probably did something silly stemming from not fully understanding the component life cycle.
I can see the words on the tiles before clicking them.