Sli*_*idy 7 react-native react-navigation
我正在使用反应导航器创建屏幕,但当屏幕打开时我无法更改标题栏的选项
我的代码(App.js):
function App() {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={{
headerTitleAlign: 'center',
headerTitle: props => <LogoTitle {...props} />,
headerRight: props => <HeaderRight {...props} />,
headerStyle: {
backgroundColor: '#F46A0D'
},
headerTintColor: '#fff',
}}>
<Stack.Screen name="Search" component={SearchScreen}/>
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
Run Code Online (Sandbox Code Playgroud)
我的 SearchScreen.js:
import React from 'react'
import { Text, View } from 'react-native'
import { Button } from 'react-native-paper'
const SearchScreen = ({ navigation }) => {
navigation.setOptions = {
headerTitle: props => <TextInput {...props} placeholder="Search" placeholderTextColor="#FFFF" style={{
fontSize: 24
}}></TextInput>,
headerRight: props => <FontAwesome5 {...props} name={'search'} size={22} color="white" style={{ marginRight: 15 }}></FontAwesome5>
}
return (
<View>
</View>
)
}
export default SearchScreen;
Run Code Online (Sandbox Code Playgroud)
在 navigation.setOptions 行中没有任何反应,默认 screenOptions Stack.Navigator 继续
在路线文件上
<Stack.Screen
name="FuncName"
component={FuncName}
options={{
headerTitle:''
}}/>
Run Code Online (Sandbox Code Playgroud)
按下后转到 FuncName 屏幕
onPress={()=>{
props.navigation.navigate("FuncName",{title:"title"})
}}
Run Code Online (Sandbox Code Playgroud)
然后通过道具标题更改标题
const FuncName = (props) => {
useEffect(()=>{
props.navigation.setOptions({ headerTitle: props.route.params.title })
},[])
}
Run Code Online (Sandbox Code Playgroud)
尝试改变这个
navigation.setOptions = {
headerTitle: props => <TextInput {...props} placeholder="Search" placeholderTextColor="#FFFF" style={{
fontSize: 24
}}></TextInput>,
headerRight: props => <FontAwesome5 {...props} name={'search'} size={22} color="white" style={{ marginRight: 15 }}></FontAwesome5>
}
Run Code Online (Sandbox Code Playgroud)
对此:
navigation.setOptions({
headerTitle: props => <TextInput {...props} placeholder="Search" placeholderTextColor="#FFFF" style={{
fontSize: 24
}} />,
headerRight: props => <FontAwesome5 {...props} name={'search'} size={22} color="white" style={{ marginRight: 15 }}></FontAwesome5>
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26698 次 |
| 最近记录: |