【发布时间】:2022-12-17 23:51:05
【问题描述】:
我有一对“是”“否”按钮,除非我刷新页面,否则这些按钮在单击时不会改变颜色。我的创可贴解决方案是将 refresh() 添加到 handleClick,这会在每次单击按钮时强制刷新整个页面。虽然这确实改变了按钮的颜色,但我更希望页面不必在每次单击按钮后刷新并加载到顶部,并且每个按钮在单击时无缝地改变颜色。
我尝试使用 this answer 中的 e.preventDefault() 来阻止整页刷新,但我不确定在我的情况下在哪里或如何使用它,因为我没有将事件传递给处理程序。
<React.Fragment>
<Button
style={{ margin: '0px 5px' }}
variant="contained"
onClick={() =>
handleClick(row.insight_id, row.device_id, 1)
}
color={
row.userConfirmed == null
? 'default'
: row.userConfirmed === 1
? 'primary'
: 'default'
}
>
Yes
</Button>
<Button
style={{ margin: '0px 5px' }}
variant="contained"
onClick={() =>
handleClick(row.insight_id, row.device_id, 0)
}
color={
row.userConfirmed == null
? 'default'
: row.userConfirmed === 1
? 'default'
: 'primary'
}
>
No
</Button>
</React.Fragment>
const handleClick = (insightId, deviceId, value) => {
dispatch(updateUserConfirm(insightId, deviceId, value));
refresh();
};
编辑:添加减速器以及row来自哪里
const userInsightsReducer = (state = initialState, action) => {
switch (action.type) {
.
.
.
case UPDATE_USER_CONFIRM_BEGIN:
return {
...state,
result: 'BEGINNING',
};
case UPDATE_USER_CONFIRM_SUCCESS:
const userInsightsDataNew = state.userInsightsData
const insightIndex = state.userInsightsData.findIndex(insight => insight.device_id === action.payload.deviceId && insight.insight_id === action.payload.insightId )
const updatedInsight = {...state.userInsightsData[insightIndex], userConfirmed:action.payload.value}
userInsightsDataNew[insightIndex] = updatedInsight
return {
...state,
userInsightsData : userInsightsDataNew,
result: "SUCCESS",
};
case UPDATE_USER_CONFIRM_FAILURE:
return {
...state,
result: 'FAILURE',
let row = userInsights.userInsightsData[index];
【问题讨论】:
-
我们需要查看连接到
dispatch(updateUserConfirm(insightId, deviceId, value));的reducer,我们需要查看row来自哪里。 -
如果这些在表单中,只需将按钮设置为
type='button'(或者无论如何都可以这样做),因为默认是提交 -
@NicholasTower 你知道了,已添加到编辑中。
-
@MarkSchultheiss 这些按钮是表格的一部分而不是表格,但我会试一试
标签: javascript reactjs