3 Important Things Every React Developer Should Know
React is a popular tool for building websites. To be a good React developer, you need to keep learning new things. Let's look at three important ideas that can help you write better React code.
1. Use the children Prop to Make Your App Faster
The children prop is not just for putting things inside other things. It's a powerful tool that can make your React app work better and faster.
Here's why the children prop is useful:
Let's look at an example:
// Not so good way
function Dashboard() {
const [time, setTime] = useState(new Date());
useEffect(() => {
const timer = setInterval(() => setTime(new Date()), 1000);
return () => clearInterval(timer);
}, []);
return (
<div>
<h1>{time.toTimeString()}</h1>
<SlowComponent />
</div>
);
}
// Better way
function App() {
return (
<Dashboard>
<SlowComponent />
</Dashboard>
);
}
function Dashboard({ children }) {
const [time, setTime] = useState(new Date());
useEffect(() => {
const timer = setInterval(() => setTime(new Date()), 1000);
return () => clearInterval(timer);
}, []);
return (
<div>
<h1>{time.toTimeString()}</h1>
{children}
</div>
);
}
In the better way, SlowComponent doesn't update every second when the time changes. This makes your app faster.
2. Know When to Use Refs Instead of State
In React, you can store information in two ways: state and refs. Knowing when to use each one is important.
Use refs when:
- The information doesn't need to show on the screen
Here's an example of a timer using state and ref:
领英推荐
// Using state (not as good)
function Timer() {
const [time, setTime] = useState(0);
const [isRunning, setIsRunning] = useState(false);
const [timerId, setTimerId] = useState(null);
useEffect(() => {
if (isRunning) {
const id = setInterval(() => setTime(t => t + 1), 1000);
setTimerId(id);
} else {
clearInterval(timerId);
}
return () => clearInterval(timerId);
}, [isRunning]);
const toggleTimer = () => setIsRunning(!isRunning);
return (
<div>
<p>Time: {time} seconds</p>
<button onClick={toggleTimer}>{isRunning ? 'Stop' : 'Start'}</button>
</div>
);
}
// Using ref (better)
function Timer() {
const [time, setTime] = useState(0);
const [isRunning, setIsRunning] = useState(false);
const timerIdRef = useRef(null);
useEffect(() => {
if (isRunning) {
timerIdRef.current = setInterval(() => setTime(t => t + 1), 1000);
} else {
clearInterval(timerIdRef.current);
}
return () => clearInterval(timerIdRef.current);
}, [isRunning]);
const toggleTimer = () => setIsRunning(!isRunning);
return (
<div>
<p>Time: {time} seconds</p>
<button onClick={toggleTimer}>{isRunning ? 'Stop' : 'Start'}</button>
</div>
);
}
Using a ref for the timer ID stops unnecessary updates and makes your app work better.
3. Use Named Exports to Make Your Code Easier to Manage
When you create a component in React, you can export it in two ways. Using named exports is often better than default exports.
Here's why named exports are good:
Let's look at the two ways:
// Default export (not as good)
const Button = ({ children, onClick }) => (
<button onClick={onClick}>{children}</button>
);
export default Button;
// How to use it
import Button from './Button';
// Named export (better)
export const Button = ({ children, onClick }) => (
<button onClick={onClick}>{children}</button>
);
// How to use it
import { Button } from './Button';
Named exports make it easier to keep your code organized and consistent.
Conclusion
By learning these three things - using the children prop smartly, choosing between refs and state wisely, and using named exports - you can write better React code. Remember, becoming a good React developer means always learning new things. Keep practicing and have fun coding!
Thanks for reading! If you want to read more of my articles, you can find them here. Feel free to get in touch.
I always enjoy talking and sharing experiences with other developers.
Senior Software Engineer | Backend-Focused Fullstack Developer | .NET | C# | Angular | React.js | TypeScript | JavaScript | Azure | SQL Server
3 个月Very informative
Senior Full Stack Developer | Full Stack Engineer | Javascript | NodeJS | ReactJS | Java | Spring Boot | AWS
3 个月Very helpful, thank you for sharing… ????
Fullstack Software Engineer | Java | Javascript | Go | GoLang | Angular | Reactjs | AWS
3 个月Thanks for sharing
Senior Software Engineer | Java | Spring | Kubernetes/Docker | Cloud | AWS | GCP | CI/CD | Backend
3 个月Insightful!
.NET Software Engineer | Full Stack Developer | C# | Angular | AWS | Blazor
3 个月Useful