我正在使用一个库来创建计算器,并希望将我计算出的值传递给另一个函数。
此处的库:https : //www.npmjs.com/package/react-native-calculator
有一个'onAccept'道具,其数据类型为:(value:number,text:string)=> void
我知道这个值是我需要传递的,但是我不知道如何格式化代码来适应这个值。到目前为止,我的代码位于下面的我正在调用acceptAdd函数的位置,该函数将与传递的值一起使用。我想将计算器的值传递给acceptAdd。感谢您的协助。
当前代码:
onAccept = {acceptAdd}
由于您尚未共享详细的尝试,因此让我们从正在使用的库中进行假设。假设您正在使用功能组件,但这可能类似于类组件。
使用TypeScript。
import React from 'react';
import { View } from 'react-native';
import { Calculator } from 'react-native-calculator';
// Lets Assume this is your calculator component
const App = (): JSX.Element => {
// Create handler to handle your onAccept which mirror
// the function signature from the docs.
const acceptAdd = (value: number, text: string): void => {
// Do whatever you want with the value.
console.log({ value, text });
};
return (
<View style={{ flex: 1 }}>
<Calculator style={{ flex: 1 }} onAccept={acceptAdd} />
</View>
);
};
export default App;
或使用纯JavaScript
const App = () => {
const acceptAdd = (value, text) => {
// Do whatever you want with the value.
console.log({ value, text });
};
return (
<View style={{ flex: 1 }}>
<Calculator style={{ flex: 1 }} onAccept={acceptAdd} />
</View>
);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句