Skip to content

Commit

Permalink
enhance(frontend): guideline to include best practices for memoizatio…
Browse files Browse the repository at this point in the history
…n in react components
  • Loading branch information
ardaerzin committed Dec 9, 2024
1 parent 9180a1e commit 4410069
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions agenta-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,47 @@ function App() {
)
}
```

### React Best Practices

#### Avoiding Inline Array Props

Passing inline arrays of objects with heavy content such as JSX is considered a bad practice in React. This is because it can lead to unnecessary re-renders and performance issues. When you pass an inline array, a new array is created every time the component renders, causing React to think that the prop has changed even if the content is the same.

For example, in the `AccordionTreePanel` component, the `items` prop is passed an inline array of objects with JSX content:

**Avoid this pattern:**

```javascript
<AccordionTreePanel
items={[
{
title: 'Item 1',
content: <div>Content 1</div>,
},
{
title: 'Item 2',
content: <div>Content 2</div>,
},
]}
/>
```

**Use this pattern:**

```javascript
import { useMemo } from 'react';

const items = useMemo(() => [
{
title: 'Item 1',
content: <div>Content 1</div>,
},
{
title: 'Item 2',
content: <div>Content 2</div>,
},
], []);

<AccordionTreePanel items={items} />
```

0 comments on commit 4410069

Please sign in to comment.