【问题标题】:react navigation navigationoptions functional component hook反应导航导航选项功能组件钩子
【发布时间】:2020-09-05 18:50:56
【问题描述】:

对不起,标题的措辞不好,我在功能组件中使用 react-navigation 的 navigationOptions 有问题,我正在使用自定义挂钩进行主题化。 我正在包装组件,然后使用 withTheme 钩子。

问题本身就是无论我如何使用 navigationOptions,它都不会改变标题

这是我遇到困难的组件:

HomeScreen = ({ theme }) => {
  return (
   ...
  );
};

HomeScreen.navigationOptions = {
  title: 'Dashboard',
  headerTintColor: 'white',
  headerStyle: {
    borderBottomWidth: 0,
    backgroundColor: 'red',
  },
  headerLeft: (
    <TouchableOpacity
      style={{ paddingLeft: 15 }}
      onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
    >
      <Feather name="arrow-left" size={24} color="#ffffff" />
    </TouchableOpacity>
  ),
  headerRight: (
    <View style={{ flexDirection: 'row' }}>

    </View>
  ),
}

const style = StyleSheet.create({
  ...
}
});

export default withTheme(HomeScreen);

还有themeProvider组件

const STORAGE_KEY = 'THEME_ID';
const ThemeContext = React.createContext();

export const ThemeContextProvider = ({ children }) => {
  const [themeID, setThemeID] = useState();

  useEffect(() => {
    (async () => {
      const storedThemeID = await AsyncStorage.getItem(STORAGE_KEY);
      if (storedThemeID) setThemeID(storedThemeID);
      else setThemeID(THEMES[1].key);
    })();
  }, []);

  return (
    <ThemeContext.Provider value={{ themeID, setThemeID }}>
      {!!themeID ? children : null}
    </ThemeContext.Provider>
  );
};

export function withTheme(Component) {
  return props => {
    const { themeID, setThemeID } = useContext(ThemeContext);

    const getTheme = themeID => THEMES.find(theme => theme.key === themeID);
    const setTheme = themeID => {
      AsyncStorage.setItem(STORAGE_KEY, themeID);
      setThemeID(themeID);
    };

    return (
      <Component
        {...props}
        themes={THEMES}
        theme={getTheme(themeID)}
        setTheme={setTheme}
      />
    );
  };
}

标头图片:

谁能告诉我我错过了什么?

【问题讨论】:

  • headerLeft 和 headerRight 可以是函数,使它们动态化吗?
  • 据我所知,问题不在于 headerLeft 和 headerRight,因为我试图删除它们并且标题仍然不会进行任何更改,删除 withTheme 钩子会呈现正确的标题,但我'不完全确定在 themeProvider 组件中可以做些什么不同的事情。
  • 也许this GitHub issue 可能会有所帮助。 v3 解决方案已将navigationOptions 变成了一个函数! (我是通过googling around 找到的。)

标签: react-native react-navigation


【解决方案1】:

您使用的是 React Navigation 4.x 还是更新的 5.x?

在较新的版本中,您只需在导航器中声明屏幕时定义选项,如本升级文档https://reactnavigation.org/docs/upgrading-from-4.x/#specifying-navigationoptions-for-a-screen。

不确定版本 4,因为我个人没有在该版本中使用导航选项。

【讨论】:

  • 我正在使用 4x。问题似乎与 - withTheme 挂钩,好像我删除了 withTheme,它正确显示了标题。
  • @MikkKüttim 当你在 ThemeProvider 中使用 useState 时,你需要提供一个默认值。例如,const [theme, setTheme] = useState(null)。 reactjs.org/docs/hooks-state.html
  • themeprovider 用于更改背景颜色,而不是标题,我只想在组件本身中手动定义 navigationOptions,但是它不起作用这就是为什么我说问题出在 withTheme钩子,但我不确定它有什么问题,因为带有功能组件的裸工作流和使用相同的 navigationOptions 代码可以工作。编辑:通过裸工作流,我的意思是最少的代码。
【解决方案2】:

显然我仍在使用 react-navigation v3 而不是 v4 - 升级库后,问题消失了,我可以在抽屉组件中使用 navigationOptions 并弹出标题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2020-11-06
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多