React concepts
React concepts
Table of Contents................................................................................................................... 1
React Quick Start - Key Concepts:.......................................................................................1
Creating and Nesting Components:..................................................................................... 1
Writing Markup with JSX:...................................................................................................... 2
Adding Styles:........................................................................................................................ 2
Displaying Data:..................................................................................................................... 3
Conditional Rendering:......................................................................................................... 3
Rendering Lists:..................................................................................................................... 4
Responding to Events:.......................................................................................................... 5
Updating the Screen:............................................................................................................. 5
Using Hooks:.......................................................................................................................... 6
Sharing Data Between Components.................................................................................... 7
Scenario 1: Independent Component State....................................................................... 7
Diagram:....................................................................................................................... 7
Scenario 2: Shared State................................................................................................... 7
Diagram:....................................................................................................................... 8
Code Example:...................................................................................................................8
Key Concepts:.................................................................................................................... 9
Next Steps...............................................................................................................................9
function MyButton() {
return (
<button>I'm a button</button>
);
}
● Component names must start with a capital letter (e.g., <MyButton />).
● HTML tags must remain lowercase (e.g., <div> or <button>).
● Use export default for the main component in a file.
● For JavaScript syntax references, check MDN or javascript.info.
● JSX is a syntax extension for writing markup in React (commonly used in React
projects).
● Supported out of the box by recommended React development tools.
● Stricter than HTML:
○ Tags must always be closed (e.g., <br />).
○ Components must return a single parent element, like a <div> or <>...</>
(fragment wrapper).
function AboutPage() {
return (
<>
<h1>About</h1>
<p>Hello there.<br />How do you do?</p>
</>
);
}
Adding Styles:
Displaying Data:
● JSX allows embedding JavaScript variables into markup using curly braces {}.
● For example, {user.name} displays the value of user.name.
<h1>{user.name}</h1>
● You can use curly braces in JSX attributes to pass JavaScript values (not just
strings).
● For example, src={user.imageUrl} uses the value of user.imageUrl for the
src attribute.
● Complex expressions can also be used inside JSX curly braces (e.g., string
concatenation).
● The style attribute uses a regular JavaScript object inside curly braces to apply
styles dynamically.
Conditional Rendering:
● React uses standard JavaScript techniques for conditions (no special syntax).
● Use an if statement to conditionally include JSX.
let content;
if (isLoggedIn) {
content = <AdminPanel />;
} else {
content = <LoginForm />;
}
return <div>{content}</div>;
● Use the conditional ? operator for more compact code inside JSX.
<div>
{isLoggedIn ? <AdminPanel /> : <LoginForm />}
</div>
<div>
{isLoggedIn && <AdminPanel />}
</div>
Rendering Lists:
● Use JavaScript features like for loops and map() to render lists of components.
● Example: an array of products:
const products = [
{ title: 'Cabbage', id: 1 },
{ title: 'Garlic', id: 2 },
{ title: 'Apple', id: 3 },
];
return <ul>{listItems}</ul>;
● Each list item (<li>) must have a key attribute to uniquely identify it.
● The key usually comes from the data (e.g., database ID).
● React uses key to track changes when inserting, deleting, or reordering items.
<li
key={product.id}
style={{ color: product.isFruit ? 'magenta' : 'darkgreen' }}
>
{product.title}
</li>
Responding to Events:
function handleClick() {
alert('You clicked me!');
}
● useState returns two values: current state (count) and the function to update it
(setCount).
● Convention: [stateVariable, setStateFunction].
● Pass initial value to useState (e.g., 0 for count).
function handleClick() {
setCount(count + 1);
}
<MyButton />
<MyButton />
● Clicking one button doesn’t affect others; each button remembers its own count.
Using Hooks:
Diagram:
MyApp
└── MyButton (count: 0)
└── MyButton (count: 0)
Code Example:
Key Concepts:
● Lifting state up: Move state to the parent and pass it down as props.
● Props: Data passed from parent to child components (e.g., count, onClick).
Next Steps