react-native - 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-native - 如何在 FlatList 中使用状态?
- java - 我可以更改另一个类的构造函数传递的属性的值吗?
- rest - 这个 GET 调用应该返回 204 还是 200 和 body?
- reactjs - TypeError:无法读取未定义的属性“nam”
- xamarin - 我如何在 Xamarin Android 中获取 Uni Code
- c++ - 如何使用 C++ 在 Solaris 中获取活动端口信息(重新创建“netstat 命令”)?
- excel - 如何导出多张工作表并将它们保存在新创建的与工作簿标题相同的文件夹中
- excel - 下面的excel公式在最后一列有问题
- sql - 将 YAML 转换为 SQL 语句
- reactjs - 打字稿:为箭头函数的对象参数定义类型