Follow along as we build a reusable component library using React and styled-components, focusing on useState for state management and props for component communication.
In this code-along, you'll build a component library that includes:
// Example of what you'll create:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div className="counter">
<h2>Counter: {count}</h2>
<Button onClick={() => setCount(count - 1)}>Decrease</Button>
<Button primary onClick={() => setCount(count + 1)}>Increase</Button>
<Button secondary onClick={() => setCount(0)}>Reset</Button>
</div>
);
}
Note: Please visit the platform to access the current code-along materials and videos.
Learn advanced state management techniques using the useEffect hook in React, including data fetching, handling side effects, and working with external APIs.
In this code-along, you'll build a data-driven application that includes:
// Example of what you'll create:
import React, { useState, useEffect } from 'react';
function UserDirectory() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [search, setSearch] = useState('');
useEffect(() => {
async function fetchUsers() {
try {
setLoading(true);
const response = await fetch('https://api.example.com/users');
if (!response.ok) throw new Error('Failed to fetch');
const data = await response.json();
setUsers(data);
setError(null);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
}
fetchUsers();
}, []); // Empty dependency array means this runs once on mount
// Filter users based on search term
const filteredUsers = users.filter(user =>
user.name.toLowerCase().includes(search.toLowerCase())
);
return (
<div>
<h1>User Directory</h1>
<input
type="text"
placeholder="Search users..."
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
{loading && <p>Loading users...</p>}
{error && <p>Error: {error}</p>}
<ul>
{filteredUsers.map(user => (
<li key={user.id}>{user.name} - {user.email}</li>
))}
</ul>
</div>
);
}
Note: Please visit the platform to access the current code-along materials and videos.