DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 121 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 121 of my software engineering marathon! Today, I unlocked a massive performance optimization tool in React.js: migrating form capture workflows away from constant state re-renders into memory-efficient DOM references using the useRef Hook! ⚛️⚡ Performance

Up until yesterday, I was tracking form data using useState, which forced my input components to completely re-render on every single keystroke. Today, I upgraded my Todo application to use uncontrolled forms, keeping the UI light and lightning-fast!


🛠️ Deconstructing the Day 121 Uncontrolled Form Architecture

As captured inside my source workspace across "Screenshot (270).png" and "Screenshot (271).png", the data lifecycle utilizes pure reference models:

1. Initializing Element References

  • Instead of creating local string spaces that trigger updates, I instantiated persistent element reference hooks on Lines 6–7 inside AddTodo.jsx:

javascript
  const todoNamePass = useRef(null);
  const todoDatePass = useRef(null);const handleOnAddTodoClick = (event) => {
    event.preventDefault();
    const todoName = todoNamePass.current.value;
    const todoDate = todoDatePass.current.value;

    todoNamePass.current.value = "";
    todoDatePass.current.value = "";
    onClickAddTodo(todoName, todoDate);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)