This document provides examples of common patterns and techniques used in React including:
- Defining React components with JSX syntax
- Transforming JSX to JavaScript using Babel
- Passing props and children to components
- Handling events and state in components
- Best practices for keys, prop validation, and component lifecycle methods
38. Attribute
// Input (JSX):
var person = <Person name={window.isLoggedIn ? window.name : ''} />;
// Output (JS):
var person = React.createElement(
Person,
{name: window.isLoggedIn ? window.name : ''}
);
39. Boolean
// These two are equivalent in JSX for disabling a button
<input type="button" disabled />;
<input type="button" disabled={true} />;
// And these two are equivalent in JSX for not disabling a button
<input type="button" />;
<input type="button" disabled={false} />;
40. Children
var content = <Container>{window.isLoggedIn ? <Nav /> : <Login />}</Container>;
// Output (JS):
var content = React.createElement(
Container,
null,
window.isLoggedIn ? React.createElement(Nav) : React.createElement(Login)
);
41. HTML Tags vs. React Components
• class => className
• for => htmlFor