javascript - 使用子组件和 react-redux (React typesciprt)
问题描述
如果电子邮件经过验证(来自 redux 的道具),则会VerifiedComp
呈现组件。children
如果电子邮件未通过验证,则会显示一个按钮。我遇到的问题是打字稿要求父组件中的孩子。
这是CreatePage
父组件的组件。
type StateTypes = {
history: { push: (link: string) => {} };
};
const mapStateToProps = ({ history }: StateTypes) => ({
history,
});
const connector = connect(mapStateToProps, { startCreateMovie });
type PropsFromRedux = ConnectedProps<typeof connector>;
export const CreatePage = ({ history, startCreateMovie }: PropsFromRedux) => {
return (
<VerifiedComp>
<CreateForm ... />
</VerifiedComp>
);
};
这是VerifiedComp
.
type PropTypes = {
children: React.ReactNode
};
type StateTypes = {
auth: { user: { emailVerified: boolean }, providerData: {email: string} };
}
const mapStateToProps = (state: StateTypes, props: PropTypes) => ({
email: state.auth.providerData.email,
children: props.children,
emailVerified: state.auth.user.emailVerified
});
const connector = connect(mapStateToProps);
type PropsFromRedux = ConnectedProps<typeof connector>;
const VerifiedComp: React.FC<PropsFromRedux> = (props: PropsFromRedux) => {
return props.emailVerified ? (
<React.Fragment>{props.children}</React.Fragment>
) : (
<Box>.....</Box>
)
这是完整的问题
Type '{ children: Element; }' is missing the following properties from type '{ email: string; emailVerified: boolean; }': email, emailVerified TS2739
19 | export const CreatePage = ({ history, startCreateMovie }: PropsFromRedux) => {
20 | return (
> 21 | <VerifiedComp>
| ^
22 | <MovieForm
解决方案
这是因为您已将儿童财产设为强制性
type PropTypes = {
children: React.ReactNode
};
而是将其设为可选,如下所示:
type PropTypes = {
children?: React.ReactNode
};
推荐阅读
- symfony - Symfony formbuilder DateTime 显示错误的日期
- laravel - Laravel 为更新方法传递 2 个参数
- java - 关闭到 RabbitMQ 的连接
- xml - 使用 XSLT 的多行
- angular - Angular:如果为 0,则检查数组长度
- javascript - 如何根据搜索关键字对字符串数组进行排序?
- ruby-on-rails - Rails ActiveStorage:链接到云端
- coq - 在 Coq 中正确地进行双重归纳
- memory - 在打开的项目窗口之间共享 Intellij IDEA 的 ls -Xmx 内存限制?
- selenium - selenium.common.exceptions.WebDriverException:消息:进程意外关闭,状态为 1