This document provides an overview of using React with Meteor to build web applications. It covers key React concepts like JSX, setting up a Meteor app with React, rendering React components to the DOM, updating component state and re-rendering, and conditional rendering based on state values. The examples demonstrate simple rendering, updating text via an input field and state, and showing/hiding an input field using a checkbox to conditionally render components.
4. JSX
● JSX is a syntax extension to JavaScript.
● Its tag syntax is neither a string nor HTML, e.g.
const element = <h1>Hello, world!</h1>;
● You can embed any JavaScript expression in JSX
● By wrapping it in curly braces, e.g.
○ {2 + 2}
5. JSX
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}
After compilation, JSX expressions become regular JS objects.
So we can:
● Use JSX inside if statements and for loops
● Assign it to variables
6. JSX
Specifying Attributes with JSX:
● String literals with quotes:
const element = <div className="container"></div>;
● JS expressions with curly braces:
const element = <img src={user.avatarUrl}></img>;
● React DOM uses camelCase property naming convention
12. React DOM
What is React DOM?
● React DOM uses Virtual DOM concept
● It is used to manipulate the contents of our app view
● DOM-specific methods can be used at the top level of
our app
● Used via react-dom package
13. ReactDOM.render()
DOM Specific React Method:
● Renders a React element into the DOM in the supplied
container
● Return a reference to the component
● Controls the contents of the container node you pass
in.
14. Rendering an Element into the DOM
● Create a <div> in your HTML file:
○ <div id="app"></div>
● This is a "root" DOM node
● Everything inside it will be managed by React DOM.
21. Updating React Element
(Text box, update state)
Add input box:
// imports/ui/components/App.jsx
<div id="container">
<b>Dynamic Text:</b> {text}
<br />
<input
type="text"
name="updateText"
value={text}
onChange={this.updateText.bind(this)}
/>
</div> See the React DOM
updates in action!
22. Updating React Element
(Text box, update state)
Add state handler:
// imports/ui/components/App.jsx
export default class App extends React.Component {
constructor(props) {
// ...
}
updateText(e) {
const text = e.target.value;
this.setState({ text });
}
render() {
// …
}
}
23. React Only Updates What's Necessary
React DOM:
● Compares the element and its children to the
previous one
● Only applies the DOM updates necessary
● To bring the DOM to the desired state.
26. Conditional Rendering
(Checkbox to show text)
render() {
const { text, show } = this.state;
// ...
return (
<div id="container">
{/* ... */}
{/* Checkbox to show input text box */}
<b>Show Input Box:</b>
<input
type="checkbox"
name="showInputBox"
checked={show}
onChange={this.toggleInputBox.bind(this)}
/>
<br /><br />
{/* ... */}
</div>
);
}
}
27. Conditional Rendering
(Checkbox to show text)
// State handler for checkbox
toggleInputBox(e) {
const show = e.target.checked;
this.setState({ show });
}
28. Conditional Rendering
(Checkbox to show text)
<div id="container">
{/* ... */}
{/* Conditionally render input text box based on checkbox value */}
{
show
? <div>
<input
type="text"
name="updateText"
value={text}
onChange={this.updateText.bind(this)}
/>
<br />
</div>
: null
}
{/* ... */}
</div>
See the Conditional
Rendering in action!
Notas do Editor
Applications built with React usually have a single root DOM node.To render a React element into a root DOM node, pass both to ReactDOM.render()Now we will see how this is done...