navigation.setOptions不变

时间:2020-04-22 16:56:58

标签: react-native react-navigation

我正在使用react-navigator创建屏幕,但是在屏幕打开时我无法更改页眉栏的选项

我的代码(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;

我的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;

在navigation.setOptions行中什么都没有发生,并且默认的screenOptions Stack.Navigator继续

2 个答案:

答案 0 :(得分:4)

尝试更改此内容

    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>
    }

对此:

    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>
    })

答案 1 :(得分:0)

在路线文件上

onPress={()=>{
    props.navigation.navigate("FuncName",{title:"title"})
  }}

在按进入FuncName屏幕

const FuncName = (props) => {
useEffect(()=>{
      props.navigation.setOptions({ headerTitle: props.route.params.title  })
  },[])
}

然后通过道具标题更改标题

    "sourceInfo": {
  "description": "(Required) Blah.",
  "type": "StringMap",
  "displayType": "textarea",
  "default": {
    "path": "https://mybucket-public.s3-us-west-2.amazonaws.com/automation.sh"
  }
}