The functionality of the checkbox works perfectly when we do state mutation by {…state}…
- User
The functionality of the checkbox works perfectly when we do state mutation by {…state} import { CreditChargesPaymentTypes, PaymentTableState, initialState } from './paymentTableStateTypes'; import { ModuleState } from '@txp-core/runtime'; import { CHARGES_TABLE_DATA_FETCHED, PaymentChargesTableActionsType, CHARGES_TABLE_DATA_UPDATE, STATE_ID } from './paymentTable_actions'; export interface paymentChargesTableState { paymentChargesTable: PaymentTableState; } const paymentTransactionTable = ( state: paymentChargesTableState = { paymentChargesTable: initialState }, action: PaymentChargesTableActionsType ): paymentChargesTableState => { switch (action.type) { case CHARGES_TABLE_DATA_FETCHED: return { ...state, paymentChargesTable: action.tableData }; case CHARGES_TABLE_DATA_UPDATE: return updateChargesTable(action.txnId, action.payload)(state); default: return state; } }; const updateChargesTable = (txnId: number, payload: Partial) => (state: paymentChargesTableState) => { const newState = { ...state }; const chargesTable = newState.paymentChargesTable.charges; const index = chargesTable.findIndex( (val: CreditChargesPaymentTypes) => val.txnId === txnId ); chargesTable[index] = { ...chargesTable[index], ...payload }; const updatedState = { ...newState, paymentChargesTable: { ...newState.paymentChargesTable, charges: chargesTable } }; console.log('updatedState', updatedState); console.log('state', state); return updatedState; }; export default paymentTransactionTable; export type PaymentChargesTableModuleState = ModuleState<typeof STATE_ID, paymentChargesTableState>; export const paymentTableSelectors = { getCharges: (state: PaymentChargesTableModuleState) => state?.[STATE_ID]?.paymentChargesTable?.charges, getCredits: (state: PaymentChargesTableModuleState) => state?.[STATE_ID]?.paymentChargesTable?.credits, getCustomerBalanceAmount: (state: PaymentChargesTableModuleState) => state?.[STATE_ID]?.paymentChargesTable?.customerBalanceAmount };
But when we do state mutation by deepclone(state) its breaks import { CreditChargesPaymentTypes, PaymentTableState, initialState } from './paymentTableStateTypes'; import { ModuleState } from '@txp-core/runtime'; import { CHARGES_TABLE_DATA_FETCHED, PaymentChargesTableActionsType, CHARGES_TABLE_DATA_UPDATE, STATE_ID } from './paymentTable_actions'; import { cloneDeep } from '@txp-core/basic-utils'; export interface paymentChargesTableState { paymentChargesTable: PaymentTableState; } const paymentTransactionTable = ( state: paymentChargesTableState = { paymentChargesTable: initialState }, action: PaymentChargesTableActionsType ): paymentChargesTableState => { switch (action.type) { case CHARGES_TABLE_DATA_FETCHED: return { ...state, paymentChargesTable: action.tableData }; case CHARGES_TABLE_DATA_UPDATE: return updateChargesTable(action.txnId, action.payload)(state); default: return state; } }; const updateChargesTable = (txnId: number, payload: Partial) => (state: paymentChargesTableState) => { const newState = cloneDeep(state); const chargesTable = newState.paymentChargesTable.charges; const index = chargesTable.findIndex( (val: CreditChargesPaymentTypes) => val.txnId === txnId ); chargesTable[index] = { ...chargesTable[index], ...payload }; const updatedState = { ...newState, paymentChargesTable: { ...newState.paymentChargesTable, charges: chargesTable } }; console.log('updatedState', updatedState); console.log('state', state); return updatedState; }; export default paymentTransactionTable; export type PaymentChargesTableModuleState = ModuleState<typeof STATE_ID, paymentChargesTableState>; export const paymentTableSelectors = { getCharges: (state: PaymentChargesTableModuleState) => state?.[STATE_ID]?.paymentChargesTable?.charges, getCredits: (state: PaymentChargesTableModuleState) => state?.[STATE_ID]?.paymentChargesTable?.credits, getCustomerBalanceAmount: (state: PaymentChargesTableModuleState) => state?.[STATE_ID]?.paymentChargesTable?.customerBalanceAmount };
- Assistant
The issue seems to lie in the
handleCheckboxClickedfunction. ThehandleCheckboxClickedfunction sets the state withsetIsCheckedWhenPaymentAmountusing a callback function, but it then tries to immediately access that updated state value. The issue here is that the state updates in React are asynchronous, which means that the value of the state might not be immediately updated after calling . - User
by above checkbox is checking and unchekcing correctly,but the amount and payment amount is not updating
- Assistant
I apologize for the confusion. In that case, let's also make sure that the
paymentAmountvalue is being updated with the latest value while updating the state.Please update the
handleCheckboxClickedfunction as follows:const handleCheckboxClicked = (txnId) => { const updatedIsChecked = !isCheckedWhenPaymentAmount; setIsCheckedWhenPaymentAmount(updatedIsChecked); console.log('hello4', isCheckedWhenPaymentAmount, updatedIsChecked); if (!updatedIsChecked) { const value = outstandingTableLines .filter((x) => x.txnId === txnId)[0] .openBalance; const updatedPaymentAmount = value.toString(); setPaymentAmount(updatedPaymentAmount); console.log('hello8', updatedPaymentAmount); const calcAmount = Number(amount) + Number(value); handleAmountUpdate(calcAmount.toString()); onPaymentFieldUpdated(txnId, { linkedPaymentAmount: updatedPaymentAmount, isLinkedPaymentAmount: true, }); } else { const updatedPaymentAmount = ''; setPaymentAmount(updatedPaymentAmount); const value1 = outstandingTableLines .filter((x) => x.txnId === txnId)[0] .isLinkedPaymentAmount; console.log('123', value1); if (value1) { const value = outstandingTableLines .filter((x) => x.txnId === txnId)[0] .linkedPaymentAmount; if (value) { const calcAmount = Number(amount) - Number(value); handleAmountUpdate(calcAmount.toString()); onPaymentFieldUpdated(txnId, { linkedPaymentAmount: updatedPaymentAmount, }); } } } };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.