我想在React Native中的导航标题上添加一个筛选图标,但我对此一无所知。

huangapple go评论47阅读模式
英文:

I want to add a filter icon on navigation header in react-native but i dont have any idea about it

问题

在导航标题中添加图标时出现错误,错误信息是对象不是一个函数。

<Stack.Screen
  name="Address"
  component={Address}
  options={{ headerLeft: <MaterialCommunityIcons name="heart" /> }}
/>
英文:

here i try to add icon in navigation header but is show error that object is not a function

   &lt;Stack.Screen
              name=&quot;Address&quot;
              component={Address}
              options={{ headerLeft: &lt;MaterialCommunityIcons name=&quot;heart&quot; /&gt; }}
            /&gt;

答案1

得分: 1

你尝试过这样吗:

      &lt;Stack.Screen
                  name=&quot;Address&quot;
                  component={Address}
                  options={{ 
                  headerLeft: () =&gt; (
                        &lt;MaterialCommunityIcons name=&quot;heart&quot; /&gt;),
                /&gt;
英文:

Have you tried like this:-

      &lt;Stack.Screen
                  name=&quot;Address&quot;
                  component={Address}
                  options={{ 
                  headerLeft: () =&gt; (
                        &lt;MaterialCommunityIcons name=&quot;heart&quot; /&gt;),
                /&gt;

答案2

得分: 0

以下是您提供的代码的翻译部分:

import * as React from 'react';
import { View, Text, Button } from 'react-native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { NavigationContainer } from '@react-navigation/native';

const Stack = createNativeStackNavigator();

function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
      <Button
        title="Go to Search"
        onPress={() => navigation.navigate('Search')}
      />
    </View>
  );
}

function EmptyScreen() {
  return <View />;
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Group
          screenOptions={{ headerStyle: { backgroundColor: 'papayawhip' } }}>
          <Stack.Screen
            name="Home"
            component={HomeScreen}
            options={{
              headerRight: () => <MaterialCommunityIcons name="heart" />,
            }}
          />
          <Stack.Screen name="Profile" component={EmptyScreen} />
        </Stack.Group>
      </Stack.Navigator>
    </NavigationContainer>
  );
}

希望对您有所帮助!

英文:

Check below working code:

import * as React from &#39;react&#39;;
import { View, Text, Button } from &#39;react-native&#39;;
import { createNativeStackNavigator } from &#39;@react-navigation/native-stack&#39;;
import { NavigationContainer } from &#39;@react-navigation/native&#39;;

const Stack = createNativeStackNavigator();

function HomeScreen({ navigation }) {
  return (
    &lt;View style={{ flex: 1, alignItems: &#39;center&#39;, justifyContent: &#39;center&#39; }}&gt;
      &lt;Text&gt;Home Screen&lt;/Text&gt;
      &lt;Button
        title=&quot;Go to Search&quot;
        onPress={() =&gt; navigation.navigate(&#39;Search&#39;)}
      /&gt;
    &lt;/View&gt;
  );
}

function EmptyScreen() {
  return &lt;View /&gt;;
}

export default function App() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Stack.Navigator&gt;
        &lt;Stack.Group
          screenOptions={{ headerStyle: { backgroundColor: &#39;papayawhip&#39; } }}&gt;
          &lt;Stack.Screen
            name=&quot;Home&quot;
            component={HomeScreen}
            options={{
              headerRight: () =&gt;&lt;MaterialCommunityIcons name=&quot;heart&quot; /&gt;,
            }}
          /&gt;
          &lt;Stack.Screen name=&quot;Profile&quot; component={EmptyScreen} /&gt;
        &lt;/Stack.Group&gt;
      &lt;/Stack.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}

Hope it will help you!

huangapple
  • 本文由 发表于 2023年7月6日 15:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定