react-native - How do I add custom title and button to a header in React Native?
问题描述
I have a simple page with list of students. Once I click on each student profile, I want to go to a new page, where the title of the page will be the name of the student and where I will have a button on the right. All of this should be done in the header. So I put the following code:
<Stack.Screen name="Profile" component={ProfileScreen}
options={({ route }) => ({ title: route.params.name })}
/>
Which correctly displays the name of the student on the header. Then I wanted to add the button on the right and as written here I changed my code to:
<Stack.Screen name="Profile" component={ProfileScreen}
options={{ headerTitle: ({route}) => ({ title: route.params.name }),
headerRight: () => (
<Button
onPress={() => alert('This is a button!')}
title="Info"
color="#fff"
/>),
}}
/>
And now I have the following error: TypeError: undefined is not an object (evaluating 'route.params')
Can someone advice how to add both custom title and button
解决方案
You will have to pass it like below
options={({ route }) => ({
title: route.params.name,
headerRight: () => (
<Button
onPress={() => alert('This is a button!')}
title="Info"
color="#fff"
/>
),
})}
推荐阅读
- azure - 如何在 Azure Functions 中使用多个身份验证方案
- hook - 气流的 MSSQL 钩子
- linux - 重定向http请求的最佳方法是什么
- java - Drools-等效于“不在”SQL
- python - 在没有库的情况下更改句子 Python 中的字符
- android - 如何使 RTL appBarConfiguration androidx.navigation?
- python - 将日期转换为整个新列的不同格式
- angular - 在 click 方法上从另一个组件调用 app.component.ts 上的方法 - 角度 6
- ios - SwiftUI List 在导航展示后有一个奇怪的过渡
- c# - 使用自定义 WebApplicationFactory 设置集成测试项目