3 Important Things Every React Developer Should Know

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:

  • It helps you pass information to components more easily
  • It lets you change parts of your app without breaking other parts
  • It can stop parts of your app from updating when they don't need to

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

  • You want to remember something between updates, but don't want to cause an update
  • You're working with timers or directly with parts of the webpage

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:

  • They make it easier to change your code later
  • They help your code editor give you better suggestions
  • They keep the names of your components the same everywhere in your project

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.


Alexandre Germano Souza de Andrade

Senior Software Engineer | Backend-Focused Fullstack Developer | .NET | C# | Angular | React.js | TypeScript | JavaScript | Azure | SQL Server

3 个月

Very informative

回复
Ewerton Bonfim

Senior Full Stack Developer | Full Stack Engineer | Javascript | NodeJS | ReactJS | Java | Spring Boot | AWS

3 个月

Very helpful, thank you for sharing… ????

回复
JUNIOR N.

Fullstack Software Engineer | Java | Javascript | Go | GoLang | Angular | Reactjs | AWS

3 个月

Thanks for sharing

回复
Lucas Martins

Senior Software Engineer | Java | Spring | Kubernetes/Docker | Cloud | AWS | GCP | CI/CD | Backend

3 个月

Insightful!

回复
Pedro Constantino

.NET Software Engineer | Full Stack Developer | C# | Angular | AWS | Blazor

3 个月

Useful

回复

要查看或添加评论,请登录

Rubens Gouveia的更多文章

  • Server and Client Composition Patterns

    Server and Client Composition Patterns

    As developers working with Next.js, understanding how to effectively compose Server and Client Components is crucial…

    25 条评论
  • When to Use Server vs. Client Components

    When to Use Server vs. Client Components

    Next.js 13 introduced a revolutionary concept: Server Components.

    23 条评论
  • Understanding State in React

    Understanding State in React

    React has revolutionized the way we build web applications, and at the core of its power lies a concept called "state".…

    24 条评论
  • Next.js: The Revolution in the React Ecosystem

    Next.js: The Revolution in the React Ecosystem

    The Evolution of Front-End Development The world of web development is in constant evolution. From the early days of…

    18 条评论
  • Avoiding the useEffect Trap

    Avoiding the useEffect Trap

    Hello, React devs! Today we're diving into a topic that's been causing a lot of discussion in the community: the use…

    22 条评论
  • HTTP State in React

    HTTP State in React

    Hello, React developers! Today, we're diving into a concept that could revolutionize how you manage state in your…

    20 条评论
  • Enhancing SVG Icons with Gradients

    Enhancing SVG Icons with Gradients

    In the ever-evolving world of web development, creating visually appealing interfaces often requires innovative…

    37 条评论
  • 3 Code Smells That Might Be Destroying Your Code

    3 Code Smells That Might Be Destroying Your Code

    Recently, I watched a fascinating video by Junior Alves about code smells, and I felt inspired to share these ideas…

    24 条评论
  • Embracing Tailwind CSS: Discovering the Magic of the Group Class

    Embracing Tailwind CSS: Discovering the Magic of the Group Class

    My Journey into Tailwind As a React developer, I've always been comfortable with Styled Components for styling my…

    27 条评论
  • Optimizing React Performance: A Deep Dive into useMemo and useCallback

    Optimizing React Performance: A Deep Dive into useMemo and useCallback

    React's performance optimization hooks, useMemo and useCallback, are powerful tools for enhancing your application's…

    31 条评论

社区洞察

其他会员也浏览了