6 Reactjs Tips and Tricks

keep your code clan by using self-closing tags.

❌ Bad: too verbose

<MyComponent></MyComponent>

✅ Good

2 - Prefer fragments

Avoid unnecessary <div> tags. Use <> fragment to keep the DOM clean.

❌ Bad

<div> <Header /> <Main /> </div>

✅ Good

<> <Header /> <Main /> </>

3 - Spread props

Destructure props for more readable and concise components.

❌ Bad

function TodoList(props) { return ( <p>{props.item}</p> ); }

✅ Good

function TodoList({item}) { return ( <p>{item}</p> ); }

4 - Setting default props

Define default values directly within props for easy management

❌ Bad

function Card({ text, small }) { let btnText = text || "Click here"; let isSmall = small || false; ... }

✅ Good

function Card({ text = "Click here", small = false, }) { ... }

5 - Simplify String Props

Pass string props without curly braces for cleaner syntax.

❌ Bad

<Button text={"Submit"} />

✅ Good

6 - Move static data out

Keep components efficient by moving static data outside.

❌ Bad

function LevelSelectorComponent() { const LEVELS = ["Easy", "Medium", "Hard"]; return (...) }

✅ Good

const LEVELS = ["Easy", "Medium", "Hard"]; function LevelSelectorComponent() { return (...) }