- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道有没有一种方法可以使用 headerRight onPress
来执行诸如调用 Alert
之类的操作:
我在 MyPet.js
中有一个函数,代码如下:
_alert=()=>{
alert('saving')
}
在router.js
中,我有一个我用来使用如下代码进行导航的所有屏幕的列表:
export const MyPatientStack = StackNavigator({
MyPatient: {
screen: MyPatient,
navigationOptions: {
tabBarLabel: 'My Patient',
tabBarIcon: ({ tintColor, focused }) => <FontAwesome name="wheelchair" size={25} color={tintColor} />,
headerTitle: 'My Patient',
headerStyle: {
backgroundColor: '#8B0000',
},
headerLeft: <HeaderBackButton tintColor="#FFF" onPress={() => navigation.goBack()} />,
// and here I want to call the function of _alert() from MyPet.js
headerRight: <FontAwesome name='search' size={20} color="#FFF" onPress={_alert()}/>,
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
textAlign:'center',
flex:1
},
}
}
})
已经尝试过了,代码没有找到变量_alert
我怎样才能做到这一点?
欢迎任何反馈
最佳答案
由于您的导航组件没有引用您的Pet.js
组件中的内容,因此您可以尝试以下方法
在 Pet.js
组件中使用 navigationOptions
作为
// Inside the class
// Since navigationOptions have no access to this parameter of the class, therefore you need to pass them as params
static navigationOptions = ({navigation}) => {
const {params}} = navigation.state;
return {
headerRight: <FontAwesome name='search' size={20} color="#FFF" onPress={() => params.showAlert()}/> />
};
};
componentDidMount() {
this.props.navigation.setParams({
showAlert: this.alertShower
});
}
alertShower = () => Alert.alert('Success', 'Hello')
关于javascript - React Navigation - 如何从另一个文件调用函数并在 headerRight onPress 上使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49670528/
我创建了一个堆栈导航器: import {createStackNavigator} from '@react-navigation/stack'; const TheStack = createSt
我是 react-native 和 formik 的新手,我遇到了我试图建立的这个问题。 如何使用 Formik 在 headerRight 中触发函数?我有 updateCorporation 函数
我仍然试图理解这个 react 导航 5.0。 仅供引用,我正在使用 expo,现在从一页导航到另一页没有问题, 问题是当我为 headerRight 放置导航时。 我将 headerRight 放在
我想知道有没有一种方法可以使用 headerRight onPress 来执行诸如调用 Alert 之类的操作: 我在 MyPet.js 中有一个函数,代码如下: _alert=()=>{ ale
为什么这不起作用?: headerRight: this.props.navigation.navigate('MenuCQ')}/> 按下后,expo 就会崩溃,根本没有错误消息。 onPress
使用react-navigation,发现headerLeft没有响应?而且没有headerLeft,headerRight class Message extends React.Component
我想调用 headerRight 按钮点击屏幕上的函数。我在 useEffect 中将 func 作为参数传递,如下所示。 useEffect(() => { navigation.setPa
我是一名优秀的程序员,十分优秀!