React 21
React 21
and for Boolean values we can convert them into Numbers fir Sorting:
import TodoItem from "./TodoItem";
import styles from "./todolist.module.css";
export default function TodoList({ todos, setTodos }) {
const sortedTodos = todos
.slice() // sort method actually changes the existing array, to prevent we have created a copy using slice method
.sort((a, b) => Number(a.status) - Number(b.status));
// we have converted Boolean values to Number for sorting
return (
<div className={styles.todolist}>
{sortedTodos.map((item) => (
<TodoItem
key={item.name}
item={item}
todos={todos}
setTodos={setTodos}
/>
))}
</div>
);
}
In React we cannot directly use Fetch () function, we have to make an api call depending upon state. When
that input field (state) changes at that time we have to make an api call.
For that we have to use useEffect hook: Syntax:
useEffect(() => {}, []);
i.e. useEffect(callbackfunction,dependency array]
App.jsx
function App() {
const [foodData, setFoodData] = useState([]);
return (
<div className="App">
<Search foodData={foodData} setFoodData={setFoodData} />
{foodData.map((i) => (
<h1>{i.title}</h1>
))}
</div>
);
}
Nesting of component:
import { useState } from "react";
import Search from "./components/Search";
import Container from "./components/Container";
import Nav from "./components/Nav";
import "./App.css";
function App() {
const [foodData, setFoodData] = useState([]);
return (
<div className="App">
<Nav />
<Search foodData={foodData} setFoodData={setFoodData} />
<Container>
<FoodList foodData={foodData} />
</Container>
</div>
);
}
Container is a parent component and FoodList is the child component. But if we render the container in this
manner, we will get a empty container because the Container by default only returns an empty div (return
<div></div>).
To fix this we have to take the child component and render in that empty div.
By default, the child container (FoodList) is passed as props to the parent Component (Container)
Therefore, we can catch this component as props into the parent component.
i.e.
We can make use of children prop {children}
Now, we can make the container as flex to render two components side by side.
These types of component are structural component as they don’t have any Functional effect.