const [fromDate, setFromDate] = useState(moment().format('DD/MM/YY'));
function generateColumnDefs(columnDefs) {
return columnDefs.map(columnDef => {
if (columnDef.field === 'lob') {
Object.assign(columnDef, {
cellRendererFramework: lobLinkRenderer
});
}
return columnDef;
});
}
function lobLinkRenderer(params) {
return (
<a onClick={() => handleClick(params)}>{params.value}</a>
);
}
const test=()=>{
return fromDate;
};
const handleClick = (params) => {
let myDate1 = fromDate;
let myDate2 = test();
}
return (
<div>
...
<button onClick={handleClick}>Test me </button>
</div>
)
The above code is part of a React component that builds an agGrid grid. The content of a certain column is being transformed to hyperlinks through cellRendererFramework. Once clicked on these hyperlinks handleClick is invoked and populates myDate1 and myDate2. What is totally unexplained to me is why fromDate does not have its current value. In the above described case, it always have the initial value offered by useState and not the current one despite the number of changes in the state variable's value. At the same time, if clicking on Test me button, I am getting the right value for fromDate...