Rescreva essa parte do artigo com as seguintes condições: 1. Não elimine NENHUMA…
- User
Rescreva essa parte do artigo com as seguintes condições:
- Não elimine NENHUMA informação dada no artigo. 2; Conceitos ou referências devem ser mantigos em inglês. 3; Apenas transcreva o artigo para português SUPER SIMPLES para crianças, ou seja, sem palavras complicadas.
- Adicione títulos e sub-títulos quando necessário. "Rendering Techniques React Course Introduction Now that we have learned how JSX works and how to write it, this lesson will cover how we can render multiple elements and conditionally render UI in JSX.
Lesson overview This section contains a general overview of topics that you will learn in this lesson.
Render a list of elements/components in JSX. Conditionally render UI. Rendering a list of elements in JSX Let us say we want to create a component that lists multiple animals:
function App() { return ( Animals: Lion Cow Snake Lizard ); } It is perfectly acceptable, but what if we want to render more than just four? It can be tedious and long, and most of the time, we will be dealing with a data structure (like a list) rather than hard-coding each animal. You have previously learned that we can embed expressions inside JSX with curly braces. So let us do just that:
function App() { const animals = ["Lion", "Cow", "Snake", "Lizard"];
return ( Animals: {animals.map((animal) => { return {animal}; })} ); } We define an array called animals. Now inside our JSX, we use map to return a new array of li elements, adding animal as its text. It should now render the same as the previous snippet we wrote. This is because JSX has the ability to automatically render arrays. The following code is identical:
function App() { const animals = ["Lion", "Cow", "Snake", "Lizard"]; const animalsList = animals.map((animal) => {animal})
return ( Animals: {animalsList} ); } You may be curious as to what the key is in our element. We will dive into how keys work in the next lesson. But, to explain briefly, it is to let React know the identity of each element in the list. React must know this information if you are dealing with a dynamic list where you add or remove elements. Since we are only dealing with a static list, it does not matter for now.
Rendering a list of components in JSX We will use props here, and you will learn more about them in a future lesson. For now, you just need to know that props are arguments that are passed into components. We will just be writing a short implementation.
function ListItem(props) { return {props.animal} }
function List(props) { return ( {props.animals.map((animal) => { return ; })} ); }
function App() { const animals = ["Lion", "Cow", "Snake", "Lizard"];
return ( Animals: ); } We have moved our element to a different component called . It still returns the element, but we can do a lot more with it as a component.
- Assistant