T
TanStack8mo ago
foreign-sapphire

is it possible to use form values as a useMemo dependency ?

so i have this
const calculatedTotal = useMemo(() => {

const values = form.state.values;
let total = 9; // Base price

if (values.sendingMethod === "pickup") {
total += 5;
}

if (values.weight > 10) {
total += 4;
}

if (values.numberOfParcels === 2) {
total += 4;
}

if (values.deliveryType === "express") {
total += 1;
}

return total;

}, [form.state.values]);
const calculatedTotal = useMemo(() => {

const values = form.state.values;
let total = 9; // Base price

if (values.sendingMethod === "pickup") {
total += 5;
}

if (values.weight > 10) {
total += 4;
}

if (values.numberOfParcels === 2) {
total += 4;
}

if (values.deliveryType === "express") {
total += 1;
}

return total;

}, [form.state.values]);
my problem is that the calculatedTotal doesn't update when a form value changes and i just have a form with a the calculatedTotal in a div, and i need it's value to update when the user changes sendingMethod foe example.
2 Replies
metropolitan-bronze
metropolitan-bronze8mo ago
You need useStore on form.state.values then it'll work
foreign-sapphire
foreign-sapphireOP8mo ago
thanks this worked wonderfully

Did you find this page helpful?