ReactJS: “Uncaught SyntaxError: Unexpected token

UPDATE — use this instead: <script type=”text/babel” src=”https://stackoverflow.com/questions/28100644/./lander.js”></script> Add type=”text/jsx” as an attribute of the script tag used to include the JavaScript file that must be transformed by JSX Transformer, like that: <script type=”text/jsx” src=”https://stackoverflow.com/questions/28100644/./lander.js”></script> Then you can use MAMP or some other service to host the page on localhost so that all of the … Read more

How to loop and render elements in React.js without an array of objects to map?

Updated: As of React > 0.16 Render method does not necessarily have to return a single element. An array can also be returned. var indents = []; for (var i = 0; i < this.props.level; i++) { indents.push(<span className=”indent” key={i}></span>); } return indents; OR return this.props.level.map((item, index) => ( <span className=”indent” key={index}> {index} </span> )); … Read more

Visual Studio 2015 JSX/ES2015 syntax highlighting

UPDATE (2017-02) Node Tools for Visual Studio (NTVS) has been using the Salsa analysis engine since v1.2 and using NTVS is likely the path of least resistance for JSX support. https://github.com/Microsoft/nodejstools Read (and upvote) this answer for more details: https://stackoverflow.com/a/41996170/9324 ORIGINAL ANSWER I ran into the same issue and found two solutions – one using … Read more

Value of this in React event handler

This is correct behavior for JavaScript and React if you use the new class syntax. The autobinding feature does not apply to ES6 classes in v0.13.0. So you’ll need to use: <button onClick={this.handleClick.bind(this)}>Click</button> Or one of the other tricks: export default class Observer extends React.Component { constructor() { super(); this.handleClick = this.handleClick.bind(this); } handleClick() { … Read more

React / JSX Dynamic Component Name

<MyComponent /> compiles to React.createElement(MyComponent, {}), which expects a string (HTML tag) or a function (ReactClass) as first parameter. You could just store your component class in a variable with a name that starts with an uppercase letter. See HTML tags vs React Components. var MyComponent = Components[type + “Component”]; return <MyComponent />; compiles to … Read more

Why calling react setState method doesn’t mutate the state immediately?

From React’s documentation: setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value. There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains. If you want a function to be executed after … Read more