【发布时间】:2019-11-27 08:19:03
【问题描述】:
我是新手,我在尝试创建的应用程序中遇到问题。我有三个组件。因此,在主组件中,我获取数据并将数据发送到进度条组件,当进度条加载时,我想访问另一个组件的功能。如何访问其他组件的功能?
主要组件
{analysis.tontop.map(({ tone, value }) => (
<MyProgressBar
onClick={() => this.handleSentClick(tone)} //this is where I want to call the function of the sound component. or is this not the correct place to call the function of the other component?
value={value}
text={tone}
key={tone}
/>
))}
进度条组件
const MyProgressBar = ({ value, text, onClick }) => {
return (
<ProgressBar
onClick={onClick}
max={100}
now={value}
/>
</div>
);
};
我想从中访问函数的组件
function Sound(tone, enteredText) {
const handleSentClick = ({ tone, onClick }) => {
//this is the function that I want to access in the main component
console.log('something something');
};
【问题讨论】:
-
使用导出和导入,还是我弄错了?
-
你真的在
MainComponent中使用Sound组件吗?您能否再显示一些代码,以便我们知道 Sound、Progressbar 和 MainComponent 是如何实际相互使用的? -
@ErtanHasani 是的,声音组件在 mainComponent 中,但进度条在我导入的另一个文件中。它实际上是一个非常大的应用程序,所以我只是发布了我认为对于这个问题来说必要的部分
-
我认为函数 handleSentClick() 应该驻留在主组件类中,因为您正在使用此运算符进行调用
-
在声音组件中需要获取一些数据,所以我将 handleSentClick 函数放在那里,它会在数据获取完成后执行一些操作。我只是在这里添加控制台日志只是为了进行一些演示。
标签: javascript reactjs