Home > Net >  state not updating correctly with moment format in reactjs
state not updating correctly with moment format in reactjs

Time:12-29

I am using datepicker in a reactjs project but when i format the date with moment, the state is not being updated with the actual value that is selected

`

 const [dates, setDates] = useState([])
const onDateChange = (values) =>{
  setDates(values.map(item=>{
    return  moment(item).format("YYYY-MM-DD")
  }))

  console.log("dates:", dates);
}
<RangePicker
         
           onChange={onDateChange}
          />

`

CodePudding user response:

I'm not sure this is the problem but logging the value right after setState won't print the new state. If you want to see the updated state you can create a useEffect function with the dates in the dependency array.

You can read more about it here https://beta.reactjs.org/reference/react/useState#troubleshooting

CodePudding user response:

i think you should open anonymous funtion inside the setDates and type what you wan

  • Related