I have a list of buttons that change a state, a string (used to decide if a modal is displayed ans as the title for this modal). What should happen :
Points 1 and 3 work fine. But 2 is not. So I decided to put some console.logs in there. If I log inside a useEffect linked to my state : title state changes on each button click that should change it but it's never cleared.
So I logged inside the handler function (triggered when a button is clicked) :
So there it is, it doesn't make any sense to me. Here's parts of my code if it helps :
const [logsModalTitle, setLogsModalTitle] = useState("");
...
const handlingLogs = (inputClause) => {
console.log("handling logs : ", showLogsModal, logsModalTitle);
if (`${inputClause.name}(${inputClause.label})` === logsModalTitle) {
setLogsModalTitle("");
} else {
setLogsModalTitle(`${inputClause.name}(${inputClause.label})`);
}
};
...
<MyCustomButton
buttonInnerText="ERROR"
buttonSize="medium"
buttonActionFunctionOne={handlingLogs}
buttonActionPropOne={input.clauses[i]}
/>
...
useEffect(() => {
console.log(logsModalTitle);
}, [logsModalTitle]);
Any idea on what is causing this problem ?
in my opinion it is really strange code, but this may work:
const handlingLogs = (inputClause) => {
if (`${inputClause.name}(${inputClause.label})` === logsModalTitle) {
setLogsModalTitle("");
setShowLogsModal(false);
} else {
setShowLogsModal(true);
setLogsModalTitle(`${inputClause.name}(${inputClause.label})`);
}
};
and you can use just one state as showLogsModal = logsModalTitle !== "";
const [logsModalTitle, setLogsModalTitle] = useState("");
const handlingLogs = (inputClause) => {
if (`${inputClause.name}(${inputClause.label})` === logsModalTitle) {
setLogsModalTitle("");
} else {
setLogsModalTitle(`${inputClause.name}(${inputClause.label})`);
}
};
const showLogsModal = logsModalTitle !== "";