Nice to meet you, ma fren 🫡. Sorry, I ain't DEVing that much ✍️ , primarily due to the nature of maintaining Open Source projects 👷, while also gigging 💰. Anyways, stay humble like a bumblebee 🐝.
Great share! However, you can also achieve the same behavior with useState using a curried function ( also with the ability to supply a function that controls state transitions ):
import{useState}from"react";functionEditCalendarEvent(){const[event,setEvent]=useState({title:"",description:"",attendees:[],});consthandleChange=(prop)=>(e)=>{// Validate and transform event to ensure state is always valid// in a centralized way// ...setEvent({...event,[prop]:e.target.value});};return(<><inputvalue={event.title}onChange={(e)=>handleChange("title")(e)}/>
{/* ... */}</>
);}
consthandleChange=(prop)=>(e)=>{// Validate and transform event to ensure state is always valid// in a centralized way// ...setEvent(event=>({...event,[prop]:e.target.value}));};
Great share! However, you can also achieve the same behavior with
useStateusing a curried function ( also with the ability to supply a function that controls state transitions ):I've done this before but slightly different
Just to be sure I get the most up-to-date state
I also do in this way. Just adding a point, instead of
setEvent(event => ({ ...event, [prop]: e.target.value }));do
setEvent(prevEvent => ({ ...prevEvent, [prop]: e.target.value }));It's not much of a difference but you would have a better code readability.