React Challenges Flashcards
I have a list that i map on, on click I want to add new input item into that list…
state updating with the spread operator.
setItems([…items, input]);
setInput(“”);
spot the wrong things
- index{cryptocurrencyList.map((index, element) => {
<>
<td>{element.name}</td>
<td>{element.rate}</td>
<td>{numberCoin}</td>
</>
})}
In the map() function, the parameters are reversed. The correct order is:
array.map((element, index) => { … });
element refers to the current item in the array.
index is the position of the item.
spot the wrong things
- return issue{cryptocurrencyList.map((index, element) => {
<>
<td>{element.name}</td>
<td>{element.rate}</td>
<td>{numberCoin}</td>
</>
})}
Fragment Syntax Inside map()
You are trying to wrap multiple <td> elements using a React Fragment (<>…</>), which is correct. However, you forgot to return the Fragment from the map() function.
Corrected map syntax:
To fix this, add a return or use the implicit return syntax by removing {} in the arrow function.
spot the wrong things
- tr tag and key –> virtual dom hon!{cryptocurrencyList.map((index, element) => {
<>
<td>{element.name}</td>
<td>{element.rate}</td>
<td>{numberCoin}</td>
</>
})}
You cannot place multiple <tr> tags inside a single <tr> parent element. Instead, the map() function should create a separate <tr> for each item in cryptocurrencyList.
explain the error with jsx here:
{cryptocurrencyList.map((element, index) => (
<tr key={index}>
<td>{element.name}</td>
<td>1 USD= {exchangeAmount ? {element.rate} {element.code} : “”} </td>
<td>{exchangeAmount ? element.rate*exchangeAmount : (0.0000000.toFixed(8))}</td>
</tr>
))}
{element.rate} and {element.code} are being wrapped with extra curly braces. You don’t need {} inside JSX when you’re already inside curly braces.
how to put in jsx string+variable
1 USD = {exchangeAmount ? ${element.rate} ${element.code}
: “”}
Improper Return from Arrow Function:
1. explicit
2. implicit
give example
When using an arrow function with curly braces {}, you need to explicitly use the return keyword to return JSX. Alternatively, you can remove the braces to implicitly return JSX.
Option 1:
// With explicit return:
cryptocurrencyList.map((element, index) => {
return (
<React.Fragment key={index}>
<td>{element.name}</td>
<td>{element.rate}</td>
<td>{numberCoin}</td>
</React.Fragment>
);
});
// With implicit return:
cryptocurrencyList.map((element, index) => (
<React.Fragment key={index}>
<td>{element.name}</td>
<td>{element.rate}</td>
<td>{numberCoin}</td>
</React.Fragment>
));
Option 2:
// With implicit return:
cryptocurrencyList.map((element, index) => (
<React.Fragment key={index}>
<td>{element.name}</td>
<td>{element.rate}</td>
<td>{numberCoin}</td>
</React.Fragment>
));
react: mapping, key and virtual dom
you always need to use a key when mapping over a list of elements because React relies on the key to efficiently update the Virtual DOM and reconcile changes to the real DOM.
Why is key Needed?
The key helps React uniquely identify each element in a list. When the Virtual DOM is updated, React uses the key to determine:
Which elements have changed.
Which elements were added or removed.