Create a Simple JSX Element
1
| const JSX = <h1>Hello JSX!</h1>;
|
Create a Complex JSX Element
1 2 3 4 5 6 7 8 9 10 11
| const JSX = ( <div> <h1>Paragraph One</h1> <p>Paragraph Two</p> <ul> <li>a</li> <li>b</li> <li>c</li> </ul> </div> );
|
1 2 3 4 5 6 7
| const JSX = ( <div> {/* This is a block */} <h1>This is a block of JSX</h1> <p>Here's a subtitle</p> </div> );
|
Render HTML Elements to the DOM
1 2 3 4 5 6 7
| const JSX = ( <div> <h1>Hello World</h1> <p>Lets render this to the DOM</p> </div> ); ReactDOM.render(JSX, document.getElementById("challenge-node"));
|
Define an HTML Class in JSX
1 2 3 4 5
| const JSX = ( <div className="myDiv"> <h1>Add a class to this div</h1> </div> );
|
1 2 3 4 5 6 7
| const JSX = ( <div> <h2>Welcome to React!</h2> <br /> <p>Be sure to close all tags!</p> <hr /> </div> );
|
For example the line-break tag can be written as
or as
, but should never be written as
, since it doesn’t contain any content.
Create a Stateless Functional Component
1 2 3
| const MyComponent = function () { return <div>some strings</div>; };
|
Create a React Component
1 2 3 4 5 6 7 8 9 10 11 12
| class MyComponent extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>Hello React!</h1> </div> ); } }
|
Create a Component with Composition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const ChildComponent = () => { return ( <div> <p>I am the child</p> </div> ); };
class ParentComponent extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>I am the parent</h1> <ChildComponent /> </div> ); } }
|
Use React to Render Nested Components
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| const TypesOfFruit = () => { return ( <div> <h2>Fruits:</h2> <ul> <li>Apples</li> <li>Blueberries</li> <li>Strawberries</li> <li>Bananas</li> </ul> </div> ); };
const Fruits = () => { return ( <div> <TypesOfFruit /> </div> ); };
class TypesOfFood extends React.Component { constructor(props) { super(props); }
render() { return ( <div> <h1>Types of Food:</h1> <Fruits /> </div> ); } }
|
Compose React Components
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Fruits extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h2>Fruits:</h2> <NonCitrus /> <Citrus /> </div> ); } }
class TypesOfFood extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>Types of Food:</h1> <Fruits /> <Vegetables /> </div> ); } }
|
Render a Class Component to the DOM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class TypesOfFood extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>Types of Food:</h1> <Fruits /> <Vegetables /> </div> ); } }
ReactDOM.render(<TypesOfFood />, document.getElementById("challenge-node"));
|
Write a React Component from Scratch
1 2 3 4 5 6 7 8 9 10 11 12 13
| class MyComponent extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>My First React Component!</h1> </div> ); } } ReactDOM.render(<MyComponent />, document.getElementById("challenge-node"));
|
https://www.freecodecamp.org/forum/t/freecodecamp-challenge-guide-write-a-react-component-from-scratch/301424
Pass Props to a Stateless Functional Component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const CurrentDate = (props) => { return ( <div> <p>The current date is: {props.date}</p> </div> ); };
class Calendar extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h3>What date is it?</h3> <CurrentDate date={Date()} /> </div> ); } }
|
Pass an Array as Props
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| const List = (props) => { { } return <p>{props.tasks.join(", ")}</p>; { } };
class ToDo extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>To Do Lists</h1> <h2>Today</h2> <List tasks={["walk dog", "workout", "workout"]} /> <h2>Tomorrow</h2> <List tasks={["walk dog", "workout", "workout"]} /> </div> ); } }
|
Use Default Props
1 2 3 4 5 6 7 8
| const ShoppingCart = (props) => { return ( <div> <h1>Shopping Cart Component</h1> </div> ); }; ShoppingCart.defaultProps = { items: 0 };
|
Override Default Props
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const Items = (props) => { return <h1>Current Quantity of Items in Cart: {props.quantity}</h1>; };
Items.defaultProps = { quantity: 0, };
class ShoppingCart extends React.Component { constructor(props) { super(props); } render() { return <Items quantity={10} />; } }
|
Use PropTypes to Define the Props You Expect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const Items = (props) => { return <h1>Current Quantity of Items in Cart: {props.quantity}</h1>; };
Items.propTypes = { quantity: PropTypes.number.isRequired };
Items.defaultProps = { quantity: 0, };
class ShoppingCart extends React.Component { constructor(props) { super(props); } render() { return <Items />; } }
|
Access Props Using this.props
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class ReturnTempPassword extends React.Component { constructor(props) { super(props); } render() { return ( <div> <p> Your temporary password is: <strong>{this.props.tempPassword}</strong> </p> </div> ); } }
class ResetPassword extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h2>Reset Password</h2> <h3>We've generated a new temporary password for you.</h3> <h3>Please reset this password from your account settings ASAP.</h3> <ReturnTempPassword tempPassword="asa22sas" /> </div> ); } }
|
Review Using Props with Stateless Functional Components
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class CampSite extends React.Component { constructor(props) { super(props); } render() { return ( <div> <Camper /> </div> ); } }
const Camper = (props) => <p>{props.name}</p>;
Camper.defaultProps = { name: "CamperBot", };
Camper.propTypes = { name: PropTypes.string.isRequired, };
|
Create a Stateful Component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class StatefulComponent extends React.Component { constructor(props) { super(props); this.state = { name: "asas", }; } render() { return ( <div> <h1>{this.state.name}</h1> </div> ); } }
|
Render State in the User Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class MyComponent extends React.Component { constructor(props) { super(props); this.state = { name: "freeCodeCamp", }; } render() { return ( <div> <h1>{this.state.name}</h1> </div> ); } }
|
Render State in the User Interface Another Way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class MyComponent extends React.Component { constructor(props) { super(props); this.state = { name: "freeCodeCamp", }; } render() { const name = this.state.name; return ( <div> <h1>{name}</h1> </div> ); } }
|
Set State with this.setState
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class MyComponent extends React.Component { constructor(props) { super(props); this.state = { name: "Initial State", }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ name: "React Rocks!", }); } render() { return ( <div> <button onClick={this.handleClick}>Click Me</button> <h1>{this.state.name}</h1> </div> ); } }
|
Bind ‘this’ to a Class Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class MyComponent extends React.Component { constructor(props) { super(props); this.state = { text: "Hello", }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ text: "You clicked!", }); } render() { return ( <div> <button onClick={this.handleClick}>Click Me</button> <h1>{this.state.text}</h1> </div> ); } }
|
Use State to Toggle an Element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class MyComponent extends React.Component { constructor(props) { super(props); this.state = { visibility: false, }; this.toggleVisibility = this.toggleVisibility.bind(this); } toggleVisibility() { this.setState((state) => { if (state.visibility === true) { return { visibility: false }; } else { return { visibility: true }; } }); } render() { if (this.state.visibility) { return ( <div> <button onClick={this.toggleVisibility}>Click Me</button> <h1>Now you see me!</h1> </div> ); } else { return ( <div> <button onClick={this.toggleVisibility}>Click Me</button> </div> ); } } }
|
Write a Simple Counter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0, }; this.increment = this.increment.bind(this); this.decrement = this.decrement.bind(this); this.reset = this.reset.bind(this); } increment() { this.setState((state) => ({ count: state.count + 1, })); } decrement() { this.setState((state) => ({ count: state.count - 1, })); } reset() { this.setState((state) => ({ count: 0, })); } render() { return ( <div> <button className="inc" onClick={this.increment}> Increment! </button> <button className="dec" onClick={this.decrement}> Decrement! </button> <button className="reset" onClick={this.reset}> Reset </button> <h1>Current Count: {this.state.count}</h1> </div> ); } }
|