Skip to content

Commit

Permalink
Adds cloneElement section
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSonOfThomp committed May 23, 2024
1 parent 90e07a6 commit 84d1299
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions STYLEGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ Minimizes inline JavaScript logic, and creates a named function that will show u

#### Prefer

```tsx
```jsx
const handleClick = e => {
e.preventDefault();
setState(curr => !curr);
Expand All @@ -362,7 +362,7 @@ const handleClick = e => {

#### Avoid

```tsx
```jsx
<button
onClick={e => {
e.preventDefault();
Expand All @@ -381,14 +381,14 @@ Standardizes how we name and search for functions that handle events

#### Prefer

```tsx
```jsx
const handleClick = () => {};
return <button onClick={handleClick} />;
```

#### Avoid

```tsx
```jsx
const onClick = () => {};
return <button onClick={onClick} />;
```
Expand All @@ -403,18 +403,62 @@ Consolidates the number of levels of DOM

#### Prefer

```tsx
```jsx
return <></>;
```

#### Avoid

```tsx
```jsx
return <div></div>;
```

---

### Prefer render props over `cloneElement`

#### Why

Passing a render prop into a component instead of cloning a prop/child is more explicit, and makes it easier to trace a child component's state.

See [react.dev](https://react.dev/reference/react/cloneElement#passing-data-with-a-render-prop) for more details.

#### Prefer

```jsx
// MyComponent.tsx
return (
<Menu
renderTrigger={triggerProps => (
<Button {...triggerProps} leftGlyph="Beaker" />
)}
/>
);
```

```jsx
// Menu.tsx
const triggerProps = {...}
return (
<>
{renderTrigger(triggerProps)}
</>
);
```

#### Avoid

```jsx
// MyComponent.tsx
return <Menu trigger={<Button leftGlyph="Beaker">}>;
```

```jsx
// Menu.tsx
const triggerProps = {...}
return <>{React.cloneElement(trigger, triggerProps)}</>;
```

### Prefer using spread operator to avoid mutation of arrays and objects

#### Why
Expand Down

0 comments on commit 84d1299

Please sign in to comment.