function AboutPage() { return ( <> <h1>About</h1> <p>Hello there.<br />How do you do</p> <> ); }
curl -sL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh sudo bash nodesource_setup.sh sudo apt-get install -y nodejs
return ( <h1> {user.name} </h1> );
import * as React from "react"; import * as ReactDOM from "react-dom"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <p> Hello, <strong>JSX</strong> </p> );
import * as React from 'react'; import * as ReactDOM from 'react-dom'; const root = ReactDOM.createRoot(document.getElementById('root')) root.render( <div> <button /> <code /> <input /> <label /<ul /> </div> );
import * as React from 'react'; import * as ReactDOM from 'react-dom'; const root = ReactDOM.createRoot(document. getElementById('root')); root.render( <section> <header> <h1>A Header</h1> </header> <nav> Nav Item </nav> <main> <p>The main content...</p> </main> <footer> <© 2021>/small> </footer> </section> );
import * as React from "react"; import * as ReactDOM from "react-dom"; class MyComponent extends React.Component { render() { return ( <section> <h1>My Component</h1> <p>Content in my component...</p> </section> ); } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render(<MyComponent />);
import * as React from "react"; import * as ReactDOM from "react-dom"; import MyComponent from "./MyComponent"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <MyComponent> <MyComponent.First /> <MyComponent.Second /> </MyComponent> );
import * as React from 'react'; import * as ReactDOM from 'react-dom'; const enabled = false; const text = 'A Button'; const placeholder = 'input value...'; const size = 50; const root = ReactDOM.createRoot(document.getElementById('root')) root.render( <section> <button disabled={!enabled}>{text}</button> <input placeholder={placeholder} size={size} /> </section> );
import * as React from "react"; import * as ReactDOM from "react-dom"; const array = ["First", "Second", "Third"]; const object = { first: 1, second: 2, third: 3, }; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <section> <h1>Array</h1> <ul> {array.map((i) => ( <li key={i}>{i}</li> ))} </ul> <h1>Object</h1> <ul> {Object.keys(object).map((i) => ( <li key={i}> <strong>{i}: </strong> {object[i]} </li> ))} </ul> </section> );
Fragments are a way to group together chunks of markup without having to add unnecessary structure to your page. For example, a common approach is to have a React component return content wrapped in a <div> element. This element serves no real purpose and adds clutter to the DOM.
import * as React from "react"; import * as ReactDOM from "react-dom"; import WithoutFragments from "./WithoutFragments"; import WithFragments from "./WithFragments"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <div> <WithoutFragments /> <WithFragments /> </div> );
import * as React from "react"; class WithFragments extends React.Component { render() { return ( <> <h1>With Fragments</h1> <p>Doesn't have any unused DOM elements.</p> </> ); } } export default WithFragments;
import * as React from "react"; class MyComponent extends React.Component { state = { first: false, second: true, }; render() { const { first, second } = this.state;
import * as React from "react"; class MyComponent extends React.Component { state = { heading: "React Awesomesauce (Busy)", content: "Loading...", }; constructor() { super(); setTimeout(() => { this.setState({ heading: "React Awesomesauce", content: "Done!", }); }, 3000); } render() { const { heading, content } = this.state; return ( <main> <h1>{heading}</h1> <p>{content}</p> </main> ); } } export default MyComponent;