gpt4 book ai didi

javascript - 我的代码在我按下按钮之前运行

转载 作者:行者123 更新时间:2023-11-30 21:13:13 24 4
gpt4 key购买 nike

我的代码有问题。要运行算法,我创建了一个按钮,我希望它在我按下它后运行,但发生的是算法运行然后按钮功能并显示返回值。整个算法都在 Demopipeline.js 内部。有什么想法吗??

import...
const demoPipeline = require('./gaitApp/src/apps/Demopipline');

export default class runAlgos extends Component {
async try () {
const newRun = await demoPipeline.run();
console.log('this is: ');
console.log(newRun);
}

render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Button
color='yellowgreen'
title="Run Demopipeline"
onPress={this.try.bind(this)}
/>

</View>
);
}
}

最佳答案

这种模式应该有效:

class runAlgos extends Component {

let try = async () => {
// ..
}

render() {
return (
<View>
<Button
title=".."
onPress={this.try}
/>
</View>
);
}
}

您还可以使用以下内容:

<Button title=".." onPress={() => this.try()} /> 

async try () {
//..
}

但这是一个不好的做法(性能下降,因为 onPress 将在每次重新渲染时创建一个函数)

关于javascript - 我的代码在我按下按钮之前运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45922746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com