curriculum/challenges/english/blocks/review-react-basics/67487e141bb6a7140a352e12.md
JavaScript libraries and frameworks offer quick solutions to common problems and speed up development by providing pre-built code.
Libraries are generally more focused on providing solutions to specific tasks, such as manipulating the DOM, handling events, or managing AJAX requests.
A couple of examples of JavaScript libraries are jQuery and React.
Frameworks, on the other hand, provide a more defined structure for building applications. They often come with a set of rules and conventions that developers need to follow.
Examples of frameworks include Angular and Next.js, a meta framework for React.
Single page applications (SPAs) are web applications that load a single HTML page and dynamically update that page as the user interacts with the application without reloading the entire page.
SPAs use JavaScript to manage the application's state and render content. This is often done using frameworks which provide great tools for building complex user interfaces.
Some issues surrounding SPAs include:
Here is an example of a simple React component that renders a greeting message:
function Greeting() {
const name = 'Anna';
return <h1>Welcome, {name}!</h1>;
}
To use the component, you can simply call:
<Greeting />
City in a file named City.js. You can export the component using the export keyword:// City.js
function City() {
return <p>New York</p>;
}
export default City;
City component into another file, you can use the import keyword:// App.js
import City from './City';
function App() {
return (
<div>
<h1>My favorite city is:</h1>
<City />
</div>
);
}
The default keyword is used as it is the default export from the City.js file.
You can also choose to export the component on the same line as the component definition like this:
export default function City() {
return <p>New York</p>;
}
npm create vite@latest my-react-app -- --template react
This command creates a new React project named my-react-app using Vite's React template. In the project directory, you will see a package.json file with the project dependencies and commands listed in it.
cd my-react-app # path to the project directory
npm install # installs the dependencies listed in the package.json file
Once the dependencies are installed, you should notice a new folder in your project called node_modules.
The node_modules folder is where all the packages and libraries required by your project are stored.
To run your project, use the following command:
npm run dev
After that, open your browser and navigate to http://localhost:5173 to see your React application running.
To actually see the code for the starter template, you can go into your project inside the src folder and you should see the App.jsx file.
// Parent component
function Parent() {
const name = 'Anna';
return <Child name={name} />;
}
// Child component
function Child(props) {
return <h1>Hello, {props.name}!</h1>;
}
You can pass multiple props using the spread operator (...), after converting them to an object. Here's an example:
// Parent component
function Parent() {
const person = {
name: 'Anna',
age: 25,
city: 'New York'
};
return <Child {...person} />;
}
In this code, the spread operator {...person} converts the person object into individual props that are passed to the Child component.
function Greeting({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please log in</h1>}
</div>
);
}
&&) operator. This is useful when you want to render content only if a certain condition is met. Here's an example:function Greeting({ user }) {
return (
<div>
{user && <h1>Welcome, {user.name}!</h1>}
</div>
);
}
In the code above, the h1 element is only rendered if the user object is truthy.
You can also use a direct if statement this way:
function Greeting({ isLoggedIn }) {
if (isLoggedIn) {
return <h1>Welcome back!</h1>;
}
return <h1>Please sign in</h1>;
}
map() method to iterate over an array of items and return a new array of JSX elements.function NameList({ names }) {
return (
<ul>
{names.map((name, index) => (
<li key={`${name}-${index}`}>{name}</li>
))}
</ul>
);
}
function Greeting() {
return (
<h1
style={{ color: 'blue', fontSize: '24px', backgroundColor: 'lightgray' }}
>
Hello, world!
</h1>
);
}
export default Greeting;
You can also extract the styles into a separate object and reference it in the style attribute this way:
function Greeting() {
const styles = {
color: 'blue',
fontSize: '24px',
backgroundColor: 'lightgray'
};
return <h1 style={styles}>Hello, world!</h1>;
}
export default Greeting;
function Greeting({ isImportant }) {
const styles = {
color: isImportant ? 'red' : 'black',
fontSize: isImportant ? '24px' : '16px'
};
return <h1 style={styles}>Hello, world!</h1>;
}
export default Greeting;
color and fontSize styles are conditionally set based on the isImportant prop.Review the React Basics topics and concepts.