React & React Native

Actions (useActionState, useOptimistic)

Në shqip: Veprimet në React 19

ShpjegimiSQ

Në React 19, një formular mund të ketë action={funksion} që merr të dhënat dhe i dërgon. useActionState mban rezultatin dhe gjendjen „po dërgohet“, dhe useOptimistic e shfaq ndryshimin menjëherë para se të përgjigjet serveri.

EnglishEN

In React 19, a form can have action={function} that receives the data and sends it. useActionState keeps the result and the “sending” state, and useOptimistic shows the change instantly before the server answers.

Si ta mendosh

Si kur shtyp „pëlqej“ dhe zemra bëhet e kuqe menjëherë, para se të arrijë në server.

Lexoje në anglisht

Like tapping “like” and the heart turning red instantly, before it reaches the server.

Shembull kodijavascript

const [gabimi, dergo, duke] = useActionState(async (_, fd) => {
  return await api.dergo(fd.get("mesazhi"));
}, null);
<form action={dergo}><button disabled={duke}>Dërgo</button></form>

Terma të lidhur