Skip to content

Commit a6de51f

Browse files
committed
feat(question): add #174 - re-render 3
1 parent d4f174b commit a6de51f

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

challenges/174-re-render-3/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Consider the app that renders a SearchBar component, which is memoized using React.memo. The SearchBar accepts an onSearch function as a prop to update the search query.
2+
3+
**Can you answer the following**:
4+
- The SearchBar is memoized using React.memo, yet it still re-renders every time the parent component updates. Why does this happen?
5+
- What is causing the memoization of SearchBar to fail, and how would you resolve the issue?
6+
- Modify the code so that SearchBar only re-renders when it truly needs to, without breaking the functionality of the search feature.

challenges/174-re-render-3/info.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
difficulty: easy
2+
title: re-render 3
3+
type: question
4+
template: react
5+
tags: css, html, javascript
6+
author:
7+
github: jsartisan
8+
name: Pawan Kumar
9+
avatar_url: https://avatars.githubusercontent.com/u/6636360?v=4
10+
published_date: '2024-09-18'
11+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
```css styles.css
2+
body {
3+
font-family: sans-serif;
4+
}
5+
6+
h1 {
7+
font-size: 1.5rem;
8+
}
9+
```
10+
11+
```jsx App.jsx
12+
import SearchBar from "./SearchBar";
13+
import React, { useState } from "react";
14+
15+
function App() {
16+
const [query, setQuery] = useState("");
17+
const [items] = useState(["Apple", "Banana", "Orange", "Grapes"]);
18+
19+
const handleSearch = (searchTerm) => {
20+
setQuery(searchTerm);
21+
};
22+
23+
const filteredItems = items.filter((item) =>
24+
item.toLowerCase().includes(query.toLowerCase()),
25+
);
26+
27+
return (
28+
<div>
29+
<SearchBar onSearch={handleSearch} />
30+
<ul>
31+
{filteredItems.map((item, index) => (
32+
<li key={index}>{item}</li>
33+
))}
34+
</ul>
35+
</div>
36+
);
37+
}
38+
39+
export default App;
40+
41+
```
42+
43+
```jsx index.jsx
44+
import React, { StrictMode } from "react";
45+
import { createRoot } from "react-dom/client";
46+
import "./styles.css";
47+
48+
import App from "./App";
49+
50+
const root = createRoot(document.getElementById("root"));
51+
root.render(
52+
<StrictMode>
53+
<App />
54+
</StrictMode>
55+
);
56+
```
57+
58+
```html public/index.html
59+
<!DOCTYPE html>
60+
<html lang="en">
61+
<head>
62+
<meta charset="UTF-8">
63+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
64+
<title>Document</title>
65+
</head>
66+
<body>
67+
<div id="root"></div>
68+
</body>
69+
</html>
70+
```
71+
72+
```jsx SearchBar.jsx
73+
import { memo } from 'react';
74+
75+
const SearchBar = memo(({ onSearch }) => {
76+
console.log('SearchBar rendered');
77+
78+
return (
79+
<input
80+
type="text"
81+
placeholder="Search..."
82+
onChange={(e) => onSearch(e.target.value)}
83+
/>
84+
);
85+
});
86+
87+
export default SearchBar;
88+
```
89+
90+

0 commit comments

Comments
 (0)