reactjsdatatablejsxantd

antd datepicker how to make variable for return in const?


Hi i'm newbie frontend developer....

question is below down

  1. how to make return a in const onChange?
  2. if possible what is matter?
import React from 'react'
import { DatePicker } from 'antd';

var a = "";

const onChange = ( date, dateString ) => {
  console.log( dateString )
  return a = ( dateString )
};
export const monthval = () => {
  return a
};
const MonthPicker = () => {

  return (
    <>
        <DatePicker onChange={onChange} picker="month" 
                    
        />
    </>    
    )
}

export default MonthPicker


Solution

  • in react to save changing values, we use states. To save the date we need to create a state. and change it each time the onChange function calls. We create the state inside the component

    const [date, setDate] = useState();
    

    and In the on change you need to call the set function.

    const onChange = ( date, dateString ) => {
       setDate(dateString)
    };
    

    would recommend checking states out and what they are here. https://www.w3schools.com/react/react_usestate.asp