[英]React/Redux component with checkboxes does not update when click on checkbox even though I'm returning new state
I've been stuck for a while trying to make the re-render in the checkboxes to work, the variables are being updated but it's just the rendering that doesn't happen.我被困了一段时间,试图让复选框中的重新渲染工作,变量正在更新,但只是渲染没有发生。
I'm receiving a response from the backend that contains an object with an array of steps, I'm going to render a checkbox for every step if it's from a specific type.我收到来自后端的响应,其中包含带有一系列步骤的 object,如果它来自特定类型,我将为每个步骤呈现一个复选框。 As soon as I received the object, I add in every step a new property
value
to use it later for checking the checkboxes.我一收到 object,就在每一步中添加一个新的属性
value
,以便稍后用于检查复选框。
This is my reducer:这是我的减速器:
export const MyObject = (state: MyObject = defaultState, action: FetchMyObjectAction | UpdateStepsInMyObjectAction) => {
switch (action.type) {
case "FETCH_MYOBJECT":
return {
...action.payload, // MyObject
steps: action.payload.steps.map((step) => {
if (step.control.controlType === "1") { // "1" = checkbox
return {
...step,
value: step.control.defaultValues[0] === "true" ? true : false, // Adding the property value
};
}
return step;
}),
};
case "UPDATE_STEPS":
return {
...state,
steps: state.steps.map((step) => {
if (step.id === action.payload.stepId) { // if this is the checkbox to update
return {
...step,
value: action.payload.checked,
};
}
return step;
}),
};
default:
return state;
}
This is how I'm rendering the checkboxes:这就是我呈现复选框的方式:
for (let step of steps) {
if (step.control.controlType === "1") {
controls.push(
<Checkbox
label={step.displayName}
checked={step.value}
onChange={(_ev, checked) => {
callback(step.id, checked);
}}
disabled={false}
className={classNames.checkbox}
/>
);
}
}
callback
is a function that calls the reducer above for the case "UPDATE_STEPS". callback
是一个 function,它为案例“UPDATE_STEPS”调用上面的 reducer。
After inspecting the variables I can see that they are being updated properly, it's just that the re-render doesn't happen in the checkboxes, not even the first time I check the box, the check doesn't appear.检查变量后,我可以看到它们正在正确更新,只是重新渲染不会在复选框中发生,甚至在我第一次选中该框时,该复选框也不会出现。 If I move to a different component and then go back to the component with the checkboxes I can see now the checks.
如果我移动到另一个组件,然后 go 回到带有复选框的组件,我现在可以看到检查。 But if I check/uncheck within the same component, nothing happens visually.
但是,如果我在同一个组件中选中/取消选中,则视觉上不会发生任何事情。
As far as I know, I'm returning new objects for every update, so mutability is not happening.据我所知,每次更新我都会返回新对象,因此不会发生可变性。 Can you see what I'm missing?
你能看到我错过了什么吗?
Thanks!谢谢!
First I would inspect if the checkbox works with useState to manage your state.首先,我会检查复选框是否与 useState 一起使用来管理您的 state。
import { useState } from "react";
function CheckBoxForm() {
const [checked, setChecked] = useState(false);
return <Checkbox checked={checked} onChange={() => setChecked(!checked)} />;
}
Then I would check if you have wired up the reducer correctly using redux or useReducer.然后我会检查您是否使用 redux 或 useReducer 正确连接了减速器。 When you dispatch an action it should trigger a rerender.
当您调度一个动作时,它应该触发重新渲染。 For troubleshooting this using redux please refer to the redux docs: https://react-redux.js.org/troubleshooting#my-views-aren-t-updating-when-something-changes-outside-of-redux .
要使用 redux 解决此问题,请参阅 redux 文档: https://react-redux.js.org/troubleshooting#my-views-aren-t-updating-when-something-changes-outside-of-redux 。
You may be updating the object directly rather than dispatching an action using the function provided by the redux store.您可能会直接更新 object,而不是使用 redux 商店提供的 function 调度操作。 If you are using hooks, here is how you wire up your app to make sure the component props are subscribed to changing in the redux store.
如果您正在使用挂钩,这里是您如何连接您的应用程序以确保组件属性订阅 redux 商店中的更改。 You must wrap with a provider, use redux hooks like useSelector and use their provided dispatch function: https://react-redux.js.org/api/hooks
您必须使用提供程序包装,使用 redux 挂钩,例如 useSelector 并使用他们提供的调度 function: https://react-redux.js.org/api/hooks
Using useReducer is a much simpler process and will be easier to troubleshoot: https://beta.reactjs.org/reference/react/useReducer使用 useReducer 是一个更简单的过程,并且更容易排除故障: https://beta.reactjs.org/reference/react/useReducer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.