Knowledgebase

Handling Events and Forms in React Print

  • frontenddevelopment, frontend, performance, errors, guide, howto, solution, zillionkinghost
  • 0

User interaction.

HOW EVENTS WORK

Handlers are attached as props, with React managing the underlying listeners.

WHAT TO PASS

A function reference, not a call.

WHAT THAT MISTAKE LOOKS LIKE

The function running immediately on render rather than on interaction.

WHAT CONTROLLED INPUTS ARE

Inputs whose value comes from state, updated on change.

WHAT THAT PROVIDES

A single source of truth, and validation as they type.

WHAT IT COSTS

A render per keystroke, which matters in large forms.

WHAT UNCONTROLLED INPUTS ARE

Inputs managing their own value, read when needed.

WHEN TO USE THEM

Simple forms, and where performance matters.

WHAT TO PREFER FOR SUBSTANTIAL FORMS

A form library, which handles validation, errors and submission state.

WHAT TO ALWAYS DO

Preserve entered values on error Show errors beside the relevant field Disable submission while submitting

WHY THAT LAST ONE

Otherwise people click again and create duplicates.

WHAT TO REMEMBER

Client-side validation is convenience. The server must validate regardless.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot