首页 > 解决方案 > react native 中钩子的正确语法

问题描述

重构旧代码以在 React-Native 中使用钩子的正确方法是什么?

renderCustomTitle = () => {
 const { navigation, automaticOpen } = this.props;
 const { isPending } = navigation.state.params;

 if (automaticOpen) {
   const title = isPending ? i18n.t('Close, Pending') : i18n.t('Open Check');

   return <MsgTitle accessibilitLabel="Done" title={title} />;
 }

 const title = isPending ? i18n.t('Close, Pending') : i18n.t('Cart Check')

 return <ContMsgTitle title={title} />
}

之后

const renderCustomTitle = () => {
 const [navigation, setNavigation] = useState();
 const [automaticOpen, setautomaticOpen] = useState();

 useEffect(() => {
  const { isPending } = navigation.params;
  if (automaticOpen) {
    const title = isPending ? {t('Close, Pending')} : {t('Open Check')};

    return <MsgTitle accessibilitLabel="Done" title={title} />;
  }

  const title = isPending ? {t('Close, Pending')} : {t('Cart Check')}

  return <ContMsgTitle title={title} />
 })
}

在 const 中使用钩子的正确方法是什么?

标签: react-nativehook

解决方案


推荐阅读