Saturday, January 31, 2015

React Programming Pitfalls


1. When returning JSX from "render", the following ( cannot be on next line. Otherwise the following error will be thrown in browser debug console.

Invariant Violation: SearchBox.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.

$(function(){
    var SearchBox = React.createClass({
        getInitialState: function(){
          return {currInput: ''}
        },
        handleKey: function(e)
        {
            this.setState({currInput: this.state.currInput + e.key });
        },
        render: function(){
            return (                <div>
                    <input type="text" onKeyPress={this.handleKey} />
                    <div>{this.state.currInput}</div>
                </div>
            );
        }
    });

    React.render(<SearchBox />, $('.myContent')[0])
})

2. React event name is case sensitive, WebStorm intellisense's event name is all lower cases.

render: function(){
    return (
        <div>
            <input type="text" onKeyPress={this.handleKey} />
            <div>{this.state.currInput}</div>
        </div>
    );
}

Set up React IDE with WebStorm 9.0


Set up React IDE with WebStorm 9.0

   Download and Install WebStorm

   Create an empty project in WebStorm

   Install React Developer Tools for Chrome

To Enable Instant Browser Update

   Enable WebStore Live Edit
   Install JetBrains IDE Support for Chrome

To convert HTML to JSX

   Go to http://facebook.github.io/react/html-jsx.html for HTML to JSX converter.

To compile jsx into JavaScript

   Install react-tools from npm for jsx translation
 
   > npm install -g react-tools
   > jsx --watch src/ build/