diff --git a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e3a9cc78faaf4248d335.md b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e3a9cc78faaf4248d335.md index 051b5c4ea87..18301b7300b 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e3a9cc78faaf4248d335.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e3a9cc78faaf4248d335.md @@ -1,14 +1,44 @@ --- id: 6734e3a9cc78faaf4248d335 title: What Is the Role of JS Libraries and Frameworks, and Why Are They Used in the Industry? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: what-is-the-role-of-js-libraries-and-frameworks-and-why-are-they-used-in-the-industry --- # --description-- -Watch the video lecture and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +JavaScript libraries and frameworks provide pre-built code that streamlines the development process. While both libraries and frameworks serve to improve productivity and standardize coding practices, they differ in their approach and level of control they provide to developers. + +Libraries are generally more focused on providing solutions to specific tasks, such as manipulating the DOM, handling events, or managing AJAX requests. + +An example of a JavaScript library is jQuery. + +jQuery became very popular in the early 2010s and was widely used to simplify DOM manipulation, event handling, animations, effects, and more. This library also offered a rich ecosystem of plugins, which made it easy to build common web components like datepickers, sliders, and modal dialogs. + +Although jQuery is no longer as widely used as it once was, it significantly helped developers by making tasks that were complex in vanilla JavaScript much easier to implement. + +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. + +For example, Angular encourages a component-based architecture with a set of conventions and tools that provide a structured approach to organizing and building applications. Angular gives developers clear guidelines on how to structure components, manage state, handle routing, and interact with services, making it a more opinionated framework. + +Other examples of frameworks include Remix and NextJS. + +In contrast, React, a UI library, is more flexible and doesn't enforce any particular architectural pattern. React focuses primarily on the view layer and leaves a lot of the choices on application design up to the developers. + +Although libraries and frameworks are used across projects of all sizes, The choice between using them often depends on the project's requirements. Libraries offer flexibility for specific functionalities, while frameworks provide a structured approach towards complex applications. + +In the industry, libraries and frameworks are widely used for several reasons. They significantly speed up development by providing quick solutions for common problems. + +One common problem in JavaScript is working with dates and timezones. But there are libraries out there with built in solutions to help you with date manipulation, timezones, parsing and formatting of dates. + +A lot of popular libraries and frameworks are well-tested and maintained by large communities. This means they're often more practical than building the same thing from scratch. + +Libraries and frameworks follow best practices and patterns that have been proven effective in real life scenarios. This can lead to more robust and scalable applications. + +In conclusion, libraries and frameworks offer quick solutions to common problems, speed up development, and promote best practices. Understanding the differences between libraries and frameworks, and knowing when to use them is a valuable skill for any JavaScript developer. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e867bbf41cc5b11648c4.md b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e867bbf41cc5b11648c4.md index f643612a402..a63d0235072 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e867bbf41cc5b11648c4.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e867bbf41cc5b11648c4.md @@ -1,14 +1,32 @@ --- id: 6734e867bbf41cc5b11648c4 title: What Are Single Page Applications, and What Are Some Issues Surrounding Them? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: what-are-single-page-applications-and-what-are-some-issues-surrounding-them --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +Unlike traditional multi-page websites, single page applications (SPAs) load only one HTML page and dynamically update the content as the user interacts with the app, without reloading the entire page. This approach can lead to faster, more responsive applications, but it also comes a set of challenges and considerations. + +SPAs heavily use JavaScript to manage the application's state and render content. Instead of requesting new HTML pages from the server, SPAs use JavaScript to manipulate the DOM and fetch data asynchronously. This is often done using libraries and frameworks like React, Vue, or Angular, which provide great tools for building complex user interfaces. + +SPAs have some common issues. One significant issue is that screen readers may struggle with dynamically updated content. When content changes without a page reload/refresh, screen readers might not notify these changes, which makes our users unaware of updates on our web app. + +Another challenge with SPAs is with navigation and browser history. Users expect the back and forward buttons to work as they do on traditional websites. This might not work properly in SPAs because technically, the URL of the web app doesn’t change. Since, the URL of the web app doesn’t change, they can’t bookmark any specific page. Refreshing the page might reset the application to its initial state, rather than maintaining the current view. + +SPAs can pose challenges for SEO optimization. + +Search engines like Google can have difficulty indexing dynamically loaded content because they may not execute JavaScript properly or may miss content that isn’t included in the initial HTML page. This can lead to pages not being indexed correctly. + +However, modern search engines have improved their ability to crawl SPAs, and there are techniques such as server-side rendering (SSR), pre rendering, and the use of SEO-friendly URLs that can help mitigate these issues and improve SEO for SPAs. + +Performance is another consideration. SPAs load the entire application in one go, which means that if the loading time increases, users will be seeing a blank screen for a longer period of time. Now consider the scenario of users with slower internet speeds. Overall, the user experience will not be very pleasant. + +In conclusion, while Single Page Applications offer many benefits, they also present unique challenges. Developers need to be careful of accessibility, usability, SEO, and performance considerations when building SPAs. By addressing these issues, it's possible to create SPAs that are fast, responsive, and accessible to all users. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e86f590727c5e7e9ec5e.md b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e86f590727c5e7e9ec5e.md index f6891a4e86f..9932409d91a 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e86f590727c5e7e9ec5e.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e86f590727c5e7e9ec5e.md @@ -1,14 +1,30 @@ --- id: 6734e86f590727c5e7e9ec5e title: What Is React, and What Is It Commonly Used For? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: what-is-react-and-what-is-it-commonly-used-for --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +React is one of the most popular JavaScript libraries for building user interfaces and web applications. Originally developed and maintained by Facebook, React has gained huge popularity in web development due to its efficiency, flexibility, and features. + +A fundamental concept in React is the creation of reusable UI components. These components, such as buttons, cards, and avatar components, can be easily reused throughout your application. You can nest these components inside each other to build more complex, dynamic interfaces. + +One of the key advantages of React is that these custom components can update and render independently as data changes. + +Unlike traditional JavaScript, which requires direct manipulation of the DOM (Document Object Model), React uses a virtual DOM, which improves performance and efficiency. You’ll learn more about the virtual DOM and how it works in upcoming lecture videos. + +In addition to reusable components, React also provides a powerful way to manage the state of your application. State refers to the data that determines how a component renders and behaves. + +With React, you can easily track and update the state of components, ensuring that the UI reflects the most current data. You will learn more about working with state in future lecture videos. + +While there are many libraries within the JavaScript ecosystem, freeCodeCamp will mainly be focused on teaching React because of its wide spread use and demand in the industry. + +Over the next few lectures, we will dive deeper into building custom components, managing state, and more. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e879c78ee6c61db25b90.md b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e879c78ee6c61db25b90.md index a62644b74d4..2542213ad64 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e879c78ee6c61db25b90.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e879c78ee6c61db25b90.md @@ -1,14 +1,117 @@ --- id: 6734e879c78ee6c61db25b90 title: What Are Components in React, and How Do They Work? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: what-are-components-in-react-and-how-do-they-work --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +Components are the building blocks of React applications that allow developers to break down complex user interfaces into smaller, manageable pieces, making it easier to develop and maintain large-scale applications. + +The two types of components in React are functional and class based components. In modern React, developers will use functional components and all of the examples we look at today will be functional components. + +At a higher level, you can think of component like JavaScript functions that return elements describing the UI. + +This UI is described using JSX, a syntax extension for JavaScript that looks similar to HTML but allows you to write UI elements in a more declarative way. + +Let's look at an example of a React component: + +```js +function Greeting() { + const name = "John" + {/* The result will be Hello John*/} + return

Hello {name}

; +} +``` + +In this example, we've defined a component called Greeting. The curly braces `{}` inside the `h1` tags enable us to embed JavaScript expressions, allowing us to access the name variable within the h1 element. + +We are also applying a `className` called title to the `h1` element. + +But why are we using `className` instead of class like with regular HTML elements? + +Well this is because in JavaScript, `class` is a reserved name. So, we need to use `className` instead. + +We are also using a comment in JSX showing what the result will be. It is important to note that you can use regular comment syntax like this but it needs to be wrapped in curly braces in order for it to work. + +```js +{/* Block Comments */} +``` + +Another thing you might have noticed is that we are using a capital letter at the beginning of the component name. But why can’t we use all lower case letters? + +This is because React treats components with a capital letter as custom components, while elements with lowercase letters are considered built-in HTML elements. + +When React encounters a lowercase tag (like `
` or ``), it assumes it's a standard HTML element. However, if the component name starts with a capital letter, React will treat it as a user-defined component and render it accordingly. This distinction helps React differentiate between native HTML tags and components that you create. + +To use this `Greeting` component in our application, we would write something like this: + +```js + +``` + +This would render an `

` element with the text `"Hello, John"` to the page. But take a closer look at the syntax here. When we use the component, it ends with a forward slash and then the greater than symbol. + +When working with JSX, all tags and uses of components need to be explicitly closed. So if the component or tag does not have any children, then you need to explicitly close it like shown here + +```js + + +``` + +So far we have only been looking at how to render a single h1 element. But you can actually render multiple elements. + +Let’s take a look at the following example code here: + +```js +function Greeting() { + const name = "John"; + {/* This will throw an error */} + return

Hello {name}

+

Nice to meet you.

+} +``` + +We are trying to add another sentence of `"Nice to meet you"` but it is not rendering on the page correctly. + +There seems to be an error message instead. The error message says `"Adjacent JSX elements must be wrapped in an enclosing tag."` + +The reason why you are getting that error message is because multiple sibling elements need to be wrapped in a parent element. While you could wrap the h1 and p elements in a single div, there is another way to silence the error. + +React ``s are used to group elements together. + +Here is what the revised example will look like: + +```js +function Greeting() { + const name = "John"; + return ( + +

Hello {name}

+

Nice to meet you.

+
+ ); +} +``` + +You can also choose to use empty JSX tags which can serve as shorthand for Fragments. + +```js + function Greeting() { + const name = "John"; + return ( + <> +

Hello {name}

+

Nice to meet you.

+ + ); +} +``` + # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e88cc46e6dc679420040.md b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e88cc46e6dc679420040.md index 158d5b4c12c..1a8d82e1859 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e88cc46e6dc679420040.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/6734e88cc46e6dc679420040.md @@ -1,14 +1,63 @@ --- id: 6734e88cc46e6dc679420040 title: What is Vite and How Can It Be Used to Setup a New React Project? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: what-is-vite-and-how-can-it-be-used-to-setup-a-new-react-project --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +Unlike working with smaller vanilla HTML, CSS, and JavaScript projects, starting a new React project includes a few more steps to ensure that everything runs correctly. Instead of trying to setup of all of the necessary configurations by yourself, there are tools that will do that for you. + +One of the most popular tools for setting up projects is Vite. Vite, which means "fast" in French, is a build tool that aims to provide a faster development experience for modern web projects. Vite can be used with React, as well as with other libraries and frameworks like Vue or Svelte. You can even use it with vanilla JavaScript projects. + +To create a new project with Vite, you will need to use the command line. If you are using Windows machine, then you can use the Command Prompt or Windows PowerShell. If you are using a Mac, then you can use the Terminal app. + +Once you have the command line open, you can use the following command: + +```bash +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. + +You can then open up the new project and see the React boilerplate code that Vite has provided for you. + +The great thing about Vite is that it will only provide the files that are absolutely necessary to get started with your React project. + +To actually run the project as is, you will need to install the dependencies using the following commands in the command line: + +```bash + cd my-react-app + npm install +``` + +The `cd my-react-app` command ensures that you change to the correct directory where your project is located. + +The `npm install` command is used to install the dependencies needed to properly run your React project. Dependencies are libraries or packages that your React project requires in order to function correctly, such as React itself, ReactDOM, or other third-party packages that provide additional functionality. + +Running `npm install` will read the `package.json` file in your project directory and install all the dependencies listed there, allowing you to build and run your React app without missing any necessary components. + +The `package.json` file is a key configuration file in projects that contains metadata about your project, including its name, version, and dependencies. It also defines scripts, licensing information, and other settings that help manage the project and its dependencies. + +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. This folder contains the actual code for the dependencies listed in your package.json file, including both your project's direct dependencies and any dependencies of those dependencies. + +To run your project, run the `npm run dev` command and open up a new browser tab at `http://localhost:5173/`. + +You should see the starter template that Vite has provided for you. + +If you need to stop the local server, press `CTRL + C` in the command line. + +To actually see the code for this starter template, you can go into your project inside the src folder and you should see the `App.jsx` file. + +From there you can start to modify the file, save your changes and see the new changes display in the browser. + +And with that, you're ready to begin building your React application! # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/674ba6876f7ada867135bb95.md b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/674ba6876f7ada867135bb95.md index fc6c67b656f..7a3f68afd1d 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/674ba6876f7ada867135bb95.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-introduction-to-javascript-libraries-and-frameworks/674ba6876f7ada867135bb95.md @@ -1,14 +1,106 @@ --- id: 674ba6876f7ada867135bb95 title: How Can You Import and Export Components in React? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: how-can-you-import-and-export-components-in-react --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +In this lecture, we will take a look at how to import and export components in React. + +In this example, we have a `Cat` component that belongs in a file called `Cat.jsx`. For the file extension, we are using the jsx file extension because this file is mainly working with JSX. + +```js +function Cat() { + return ( +
+

Mr. Whiskers

+ Cute cats running in the grass. +
+ ); +} +``` + +This `Cat` component returns a JSX markup with a title and image for a cat called Mr.Whiskers. + +If we want to use our `Cat` component in another file, we need to first export it like this: + +```js +function Cat() { + return ( +
+

Mr. Whiskers

+ Cute cats running in the grass. +
+ ); +} + +export default Cat; +``` + +We are using the default keyword because this will be the default export from the module. You can also choose to export the component on the same line as the component definition like this: + +```js +export default function Cat() { + return ( +
+

Mr. Whiskers

+ Cute cats running in the grass. +
+ ); +} +``` + +You can choose to import child components in other parent component files. Or import them in the root component file. For this example, we will import the Cat component inside the root component file. + +Every React project will have a top-level component, typically called `App.jsx`. This file is usually located in the src directory of your project. You’ll learn more about common project layouts in a future lecture video. + +```js +export default function App() { + return ( + // return component here + ); +} +``` + +To use the `Cat` component inside the root `App` component, you will need to import it like this: + +```js +import Cat from "./Cat"; + +export default function App() { + return ( + // return component here + ); +} +``` + +Now, you can return the `Cat` component inside the `App` component like this: + +```js +import Cat from "./Cat"; + +export default function App() { + return ( + + ); +} +``` + +As you continue building your own React projects, you’ll become more comfortable organizing components, importing them where needed, and creating sophisticated UIs by composing these modular components. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/6734e3ceee2da4b0301719b7.md b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/6734e3ceee2da4b0301719b7.md index 3e862070066..b59e84054df 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/6734e3ceee2da4b0301719b7.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/6734e3ceee2da4b0301719b7.md @@ -1,14 +1,136 @@ --- id: 6734e3ceee2da4b0301719b7 title: How Do You Pass Props from Parent to Child Component in React? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: how-do-you-pass-props-from-parent-to-child-component-in-react --- # --description-- -Watch the video lecture and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +In the previous lectures, we learned how to build small components in React like this: + +```js +function Greeting() { + const developerName = "Jessica"; + return

Hi {developerName}!

; +} +``` + +We can choose to nest this component inside another parent component or the root component like this: + +```js +function App() { + return ; +} + +function Greeting() { + const developerName = "Jessica"; + return

Hi {developerName}!

; +} +``` + +While this code will run fine and display the result of Hi Jessica! on the screen, it is not that flexible of a component. + +What if we wanted to display a different name like Naomi, Tom, or Oliver? + +This is where React props comes in. + +Props, which is short for properties, is the way for Parent components to pass data down to the child component. + +Props can be of any type: strings, numbers, booleans, objects, or arrays. + +Let’s update our example from earlier to now accept a `name` prop: + +```js +function App() { + return ; +} + +export default App; + +function Greeting(props) { + console.log(props); + return

Hi {props.name}!

; +} +``` + +For the child component called `Greeting` we are now using `props.name` instead of hardcoding the name `"Jessica"`. We are also logging props to the console which is showing as an object. + +Then inside of the parent App component, we are passing the value to the name prop so it can be passed down to the child. + +The result will be the same on the screen like earlier, but now we have created a more flexible component. + +Now we have the ability to reuse the child component several times and pass in different names each time. + +```js +function App() { + return ( + <> + + + + + + ); +} +``` + +You can also choose to use object destructuring in the props to make it more readable. + +Here's how you could rewrite the `Greeting` component: + +```js +function Greeting({ name }) { + return

Hi {name}!

; +} +``` + +This code achieves the same result but makes it clearer which props the component is expecting to receive. + +Sometimes, you can have a lot of properties that you have to pass as props. Instead of passing them one by one, you can use the spread operator (`...`), after converting them to an object. + +Here is an example of a new child component called `DeveloperCard`: + +```js +function DeveloperCard({ name, age, country }) { + return ( +
+

Developer: {name}

+

Age: {age}

+

Country: {country}

+
+ ); +} +``` + +This `DeveloperCard` component accepts three props: `name`, `age`, and `country`. + +In the parent `App` component, we can use the spread syntax to pass all the properties from an object as individual props to the child component. + +```js +function App() { + const developerObj = { + name: "Alice", + age: 30, + country: "USA", + }; + + return ( +
+ +
+ ); +} +``` + +This is particularly useful when working with arrays of objects and passing multiple sets of properties to child components. For example, you might have a list of developers where each object in the array has the same structure but represents a different person. + +You will learn more about how to render lists in React in future lectures. + +Using props in React makes your components more flexible and reusable, allowing you to build more complex UIs. However, it's important to note that props are immutable, meaning they cannot be changed once passed to a component. If you need to handle user input and modify data, you should use state instead. You'll learn more about managing state in future lectures. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500abfe36cd015b67b1b7.md b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500abfe36cd015b67b1b7.md index 59701e4b40e..0ec8baed8e8 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500abfe36cd015b67b1b7.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500abfe36cd015b67b1b7.md @@ -1,14 +1,71 @@ --- id: 673500abfe36cd015b67b1b7 title: How Does Conditional Rendering Work in React Components? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: how-does-conditional-rendering-work-in-react-components --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +Conditional rendering in React allows you to create dynamic user interfaces. It is used to show different content based on certain conditions or states within your application. + +The most common approaches of using Conditional rendering includes using if statements, the ternary operator, and `logical AND (&&)` operator. + +The simplest form of conditional rendering uses an if statement. + +Here's an example: + +```js +function Greeting({ isLoggedIn }) { + if (isLoggedIn) { + return

Welcome back!

; + } + return

Please sign in

; +} +``` + +In this example, the `Greeting` component checks the `isLoggedIn` prop. If it's true, it returns a welcome message, otherwise, it prompts the user to sign in. + +Here is an example using the Greeting component inside of the parent App component. + +```js +function App() { + return ( +
+ +
+ ); +} +``` + +For simpler conditions, the ternary operator (?) is often used directly within JSX. It allows for inline conditional rendering, which can make your code more concise: + +```js +function Greeting({ isLoggedIn }) { + return

{isLoggedIn ? "Welcome back!" : "Please sign in."}

; +} +``` + +This code achieves the same result as the previous example but in a more compact form. The ternary operator checks isLoggedIn and renders the appropriate message. + +Another common pattern for conditional rendering is using the `logical AND (&&)` operator. This is particularly useful when you want to render something or nothing based on a condition: + +```js +function Notification({ message }) { + return ( +
+ {message &&

{message}

} +
+ ); +} +``` + +In this example, the paragraph element with the message is only rendered if the `message` prop is truthy. If `message` is falsy (meaning it is an empty string, `null`, or `undefined`), nothing is rendered to the screen. + +By mastering these techniques of conditional rendering, you can build more interactive and user-friendly applications that adapt to changing data and user interactions. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500b41af8500191febedc.md b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500b41af8500191febedc.md index 957731e7864..fdaf553686e 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500b41af8500191febedc.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500b41af8500191febedc.md @@ -1,14 +1,80 @@ --- id: 673500b41af8500191febedc title: How Do You Render Lists in React? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: how-do-you-render-lists-in-react --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +Rendering lists is a fundamental task in React web apps, and is used for displaying data to users. In React, the `map()` method is used to transform an array of data into an array of JSX elements that can be rendered in the UI. + +Here is an example of a component called `FruitList` that displays a list of fruits: + +```js +function FruitList() { + const fruits = ['Apple', 'Banana', 'Cherry', 'Date']; + return ( +
    + {fruits.map(fruit =>
  • {fruit}
  • )} +
+ ); +} +``` + +In this example, the `map()` function iterates over each item in the fruits array. For each fruit, it creates a new `
  • ` element containing the fruit's name. The newly created array of `
  • ` elements is then displayed inside the `
      ` parent tags. + +However, when rendering lists in React, it is important not to forget the key prop to each element in the list. The key must always be unique and it helps React identify which items have changed, been added, or been removed, which is essential for efficient rendering and updating the list. + +Let's modify our example to include keys: + +```js +function FruitList() { + const fruits = ["Apple", "Banana", "Cherry", "Date"]; + return ( +
        + {fruits.map((fruit, index) => ( +
      • {fruit}
      • + ))} +
      + ); +} +``` + +In this refactored example, we are creating a unique key for each list item by concatenating the fruit name with its index. This ensures that each list item has a distinct key, which helps React efficiently manage and update the list when items are added, removed, or reordered. + +React's also allows you to render more complex structures. For instance, you might have an array of objects representing users, each with multiple properties that you want to display: + +```js +function UserList() { + const users = [ + { id: "user-001-employee", name: "Alice", email: "alice@example.com" }, + { id: "user-002-employee", name: "Bob", email: "bob@example.com" }, + { id: "user-003-employee", name: "John", email: "john@example.com" }, + ]; + return ( +
      + {users.map((user) => ( +
      +

      {user.name}

      +

      {user.email}

      +
      + ))} +
      + ); +} +``` + +In this example, we're creating a more complex JSX structure for each user, displaying both their name and email. We're using the user's id as the key, which is a good practice. + +In conclusion, rendering lists in React involves converting arrays of data into JSX elements, typically using the `map()` function. + +Always remember to provide a unique key for each list item to help React manage the updating and rendering roles. + +With these techniques, you can create flexible, efficient, and dynamic lists in your React applications. # --questions-- diff --git a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500bfe1f41601c1ddb1a2.md b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500bfe1f41601c1ddb1a2.md index 42fc4bdbd40..5996cfba566 100644 --- a/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500bfe1f41601c1ddb1a2.md +++ b/curriculum/challenges/english/25-front-end-development/lecture-working-with-data-in-react/673500bfe1f41601c1ddb1a2.md @@ -1,14 +1,81 @@ --- id: 673500bfe1f41601c1ddb1a2 title: How Do Inline Styles Work in React? -challengeType: 11 -videoId: nVAaxZ34khk +challengeType: 19 +# videoId: nVAaxZ34khk dashedName: how-do-inline-styles-work-in-react --- # --description-- -Watch the lecture video and answer the questions below. +The video for this lecture isn't available yet, one will be available soon. Here is a transcript of the lecture for now: + +In React, inline styles are used to apply CSS styles directly to React elements within your JSX code instead of defining them in separate CSS files. + +React's approach to inline styles involves using JavaScript objects to define styles, rather than traditional CSS strings. This means that instead of writing styles as you would in a CSS file, you create a JavaScript object where the keys are camelCased versions of CSS property names, and the values are the strings of CSS values. + +Here is an example of how you can use inline styles for a `Button` component: + +```js +function Button({ buttonText }) { + const defaultStyles = { + backgroundColor: "#007BFF", + color: "white", + border: "none", + borderRadius: "4px", + padding: "10px 20px", + fontSize: "16px", + fontWeight: "bold", + cursor: "pointer", + transition: "background-color 0.3s ease", + }; + + return ; +} +``` + +In this example, we define a style object called defaultStyles. We then apply these styles to a button element using the style attribute. React takes care of applying these styles to the element when it renders. + +You can also choose to pass in an object directly to the style attribute. Here is what a revised example would look like: + +```js +function Button({ buttonText }) { + return ( + + ); +} +``` + +Sometimes you might want to pass in an object directly if there are only a few properties like shown here. Otherwise, passing in a name to an object would be better like in the first example. + +It's important to note that while CSS property names are typically written in kebab-case (like font-size), in React's inline styles, we use camelCase (like fontSize). This is because the style object is a JavaScript object, and kebab-case names are not valid as object keys in JavaScript without using quotes. + +A great advantage of inline styles in React is that they support dynamic styling based on a component state or props. For example: + +```js +function DynamicButton({ isActive }) { + const buttonStyles = { + backgroundColor: isActive ? "green" : "red", + color: "white", + padding: "10px 15px", + border: "none", + cursor: "pointer", + }; + + return ; +} +``` + +In this example, the button's background color changes based on the isActive prop. This kind of dynamic styling can be powerful for creating interactive and responsive user interfaces. + +In summary, inline styles in React provide a powerful way to apply and manipulate styles directly within your components. They use JavaScript objects instead of CSS strings, require camelCased property names, and can easily incorporate dynamic values. They're an essential tool in a React developer's toolkit, especially for creating highly customized and interactive user interfaces. # --questions--