将js-值传递给其他形式并在类中使用

问题描述 投票:0回答:1

所以我有两个文件。

第一个文件:

btn1_handle = (id_value: any) => {
receiveID(id_value);

第二文件:

    export const receiveID = (id_value: any) => {
    console.log(id_value);
    this.setState{ii: id_value};
}

但是我无法更改状态,因为导出的const在类之外。如何更改状态?

reactjs semantic-ui
1个回答
0
投票

这种方式不起作用,您只能从当前组件更新状态。因此将receiveID函数移至您的组件类。

编辑:

您应该使用React Context。看看这个:https://codesandbox.io/s/react-context-api-example-0ghhy

© www.soinside.com 2019 - 2024. All rights reserved.