当我将道具传递给子元素时,我收到以下错误:
TS2322:输入' {}'不能分配类型' IntrinsicAttributes& FooterRightSideProps& {children?:ReactNode; }&#39 ;.
中缺少
输入' {}'不能分配给' FooterRightSideProps' 物业' onClickCreate'类型' {}'。
我的代码如下:
import React from "react";
import CSSModules from "react-css-modules";
import styles from "./Footer.module.sass";
import { Icon } from "@components/icon/Icon";
import { Link } from "@components/typography/link";
import { Button } from "@components/button/Button";
export interface FooterProps {
}
export const Footer: React.SFC<FooterProps> =
CSSModules(styles)
(
(props: FooterProps) =>
<div styleName="footer">
<FooterLeftSide />
<FooterRightSide { ...props } /> //an error occurs here
</div>
);
export const FooterLeftSide: React.SFC =
CSSModules(styles)(
() =>
<div styleName="footer-left-side"></div>
);
export interface FooterRightSideProps {
onClickAbandon?: () => void;
onClickCreate: () => void;
}
export const FooterRightSide: React.SFC<FooterRightSideProps> =
CSSModules(styles)
(
(props: FooterRightSideProps) =>
<div styleName="footer-right-side">
<Link
className="option-back"
styleName="header-option"
onClick={props.onClickAbandon}
>
<div styleName="icon-with-label">
<Icon name="left" />
</div>
Abandon
</Link>
<Button
onClick={props.onClickCreate}
theme="primary-white"
>
Create Profile
</Button>
</div>
);
你们有没有想法,我怎样才能将这个onClickCreate道具传递给嵌套在父级的我的子元素?
答案 0 :(得分:0)
props
的 FooterRightSide
类型为FooterProps
,但此组件需要满足FooterRightSideProps
的道具。
答案 1 :(得分:0)
export interface FooterProps extends FooterRightSideProps {}
并将其作为道具传递给Footer,可以手动<Footer onClickCreate={someCallbackReference} onClickAbandon={optionalSomeCallbackReference} />
或者更多可能通过HOC绑定到flux / redux商店,例如用于还原...
import {connect} from 'react-redux'
import {Dispatch, bindActionCreators} from 'redux'
import * as someActions from './someActions'
import {YourApplicationStateShape} from './someStateInterfaces'
// your Footer definitions here...
// modify FooterProps in that to be...
export interface FooterProps extends FooterRightSideProps {}
export default connect(
null, // no state props need mapping in this particular case
(dispatch: Dispatch<YourApplicationStateShape>): FooterProps =>
bindActionCreators({
onClickCreate: () => someActions.createSomething()
onClickAbandon: () => someActions.abandonSomething()
}, dispatch)
)(Footer)