React:使用 JSON 数据传递函数
React: Pass Function with JSON Data
我正在尝试使用 React-Native 从 JSON 文件生成组件,我想知道将函数作为 prop
传递给组件的最佳方式是什么?
例如,button
需要道具 onPress
,那么有没有办法像我在此处所做的那样将函数传递给对象 onPress
:
{
component: "Button",
props: {
title: "This is the Title",
onPress: {() => {Alert.alert("Tapped!")}}
}
}
由于这会产生意外的令牌错误,有什么更好的方法来完成此操作?
函数声明周围有额外的大括号无效。删除那些,你应该是好的:
{
component: "Button",
props: {
title: "This is the Title",
onPress: () => {Alert.alert("Tapped!")}
}
}
我正在尝试使用 React-Native 从 JSON 文件生成组件,我想知道将函数作为 prop
传递给组件的最佳方式是什么?
例如,button
需要道具 onPress
,那么有没有办法像我在此处所做的那样将函数传递给对象 onPress
:
{
component: "Button",
props: {
title: "This is the Title",
onPress: {() => {Alert.alert("Tapped!")}}
}
}
由于这会产生意外的令牌错误,有什么更好的方法来完成此操作?
函数声明周围有额外的大括号无效。删除那些,你应该是好的:
{
component: "Button",
props: {
title: "This is the Title",
onPress: () => {Alert.alert("Tapped!")}
}
}