React:如何将函数称为状态对象值?
我有一个包含小部件的页面,每个小部件都包含用于显示数据的数据和设置。单击小部件的按钮时,侧边栏会打开(侧边栏组件是页面的孩子,小部件的兄弟姐妹)。在此侧边栏中,输入允许编辑单击的小部件数据和设置。
我为实现这一目标而做的是:我在父侧栏和小部件中创建了一个状态,当单击小部件时,设置了该状态以包含侧栏所需的数据和一个回调功能来编辑它们。
错误的是:侧边栏会尽其所能显示来自小部件的数据,但在修改数据时不会调用回调函数。
这是小部件组件中的代码:
(...)
const updatingInformations = (data, settings, config) => {
console.log("updating informations : ", data, settings, config);
data && setWidgetData(data);
settings && setWidgetSettings(settings);
config && setWidgetConfig(config);
};
(...)
<button
onClick={(e)=>{
e.preventDefault();
setSidebarContent({
type: "widget",
widget: widget,
informations: {
data: widgetData,
config: widgetConfig,
settings: widgetSettings,
},
callback: updatingInformations,
});
}}
/>
(...)
侧栏组件内部:
(...)
const updatingWidget = (path, newValue) => {
let stockInfo = { ...sidebarContent.informations };
stockInfo[path[0]][path[1]][path[2]][path[3]] = newValue;
console.log(
"updating widget : ",
sidebarContent,
stockInfo[path[0]][path[1]][path[2]][path[3]],
);
sidebarContent.callback(null, stockInfo.settings, null);
};
触发操作(编辑输入后)时,updatingwidget()
均由其console.log()< /code>但
更新信息()
未调用。
我的代码怎么了?
I have a page which contains widgets, each widget contains data and settings used to display the data. When a widget's button is clicked, a sidebar opens (the sidebar component is a child of the page, sibling of the widgets). Within this sidebar, inputs allow to edit the clicked widget data and settings.
What I did to achieve this : I created a state in the parent to both the sidebar and the widgets, when a widget is clicked, this state is setted to contain both the data needed by the sidebar and a callback function to edit them.
What is wrong : The sidebar displays the data from the widget as it should but it does not call the callback function when data is modified.
Here's the code inside the widget component :
(...)
const updatingInformations = (data, settings, config) => {
console.log("updating informations : ", data, settings, config);
data && setWidgetData(data);
settings && setWidgetSettings(settings);
config && setWidgetConfig(config);
};
(...)
<button
onClick={(e)=>{
e.preventDefault();
setSidebarContent({
type: "widget",
widget: widget,
informations: {
data: widgetData,
config: widgetConfig,
settings: widgetSettings,
},
callback: updatingInformations,
});
}}
/>
(...)
Inside the sidebar component :
(...)
const updatingWidget = (path, newValue) => {
let stockInfo = { ...sidebarContent.informations };
stockInfo[path[0]][path[1]][path[2]][path[3]] = newValue;
console.log(
"updating widget : ",
sidebarContent,
stockInfo[path[0]][path[1]][path[2]][path[3]],
);
sidebarContent.callback(null, stockInfo.settings, null);
};
When the action is triggered (after editing an input), updatingWidget()
is called as shown by its console.log()
but updatingInformations()
isn't called.
What is wrong with my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论