How to add a state value to an Object?

Viewed 39

I would like to add the value from the description Text-field to the value prop of the Object. The value={description} is stored in a state [description, setDescription]. Can someone help how to implement it correctly into the handleCreate function??

export const Dialog: React.FC<DialogProps> = ({allTasks, setAllTasks}) =>{

    const queryClient = useQueryClient();
    const [open, setOpen] = useState(false);
    const [description, setDescription] = useState(""); 
    


    const handleClickOpen = () => {
        setOpen(true);
      };
    
      const handleClose = () => {
        setOpen(false);
      };

     const handleCreate =(description: any)=>{
      const addObjectToArray = (task: any) => {
        setAllTasks ((allTasks: any) => [...allTasks, task]);
      };
      
      addObjectToArray({
        description: description,
      })
       setOpen(false);
       };

return (
    <div>

      <Dialog  open={open} onClose={handleClose}>
        <DialogTitle>Add New Task</DialogTitle>
        <DialogContent>
         <Box sx={{width: 500}}>

          <TextField
            id="description-text-field"
            value={description}
            onChange={(e) => setDescription(e.target.value)}
          />
         </Box>
        </DialogContent>
        <DialogActions>
           <Button onClick={handleClose}>Cancel</Button>
           <Button onClick={handleCreate}>Create</Button>
        </DialogActions>
      </Dialog>
    </div>

)}
1 Answers

Try this code:

const handleCreate = (description: any) => {
    const addObjectToArray = (task: any) => {
      setAllTasks([...allTasks, task]);
    };

Related