Reputation: 50810
Does the below code to update state and access in the same onCellClicked function look good ? I have "fileStatus" and "fileType" as useState in my hook and "sharedData" is my context obj.
const onCellClicked = (e) => {
if (e.data.status !== 'TO') {
console.log(e);
let fileType = (e.data.status === 'Y') ? 'O' : 'I';
setCurrentFileStatus(e.data.status);
setFileType(fileType);
var tempObj = {};
setCurrentFileStatus((fileStatus) => {
setFileType((fileType) => {
tempObj.currentFileStatus = fileStatus;
tempObj.summaryDate = form.dateField;
tempObj.fileType = fileType;
});
});
setSharedData(tempObj); // I want to ultimately set my context object based on the state values
console.log("tempObj = " + tempObj);
}
}
Upvotes: 1
Views: 160
Reputation: 28685
tempObj.fileType = fileType;
You are updating state variable in mutable way this is not recommended.
Why not do:
......
let fileTypeLocal = e.data.status === 'Y' ? 'O' : 'I';
let fileStat = e.data.status;
setCurrentFileStatus(fileStat);
setFileType(fileTypeLocal);
setSharedData({
currentFileStatus: fileStat,
summaryDate: form.dateField,
fileType: fileTypeLocal
});
?
Upvotes: 1