如何将函数组件的子组件传递给 React 类中的另一个函数组件?

时间:2021-02-02 18:30:15

标签: reactjs components react-props

class About extends React.Component<{},{}>{
    Format = () => {
        return(
            <div>
                {this.props.children}
            </div>
        )
    }

    Type1 = () => {
        return(
            <this.Format>
                <div>...</div>
            </this.Format>
        )
    }
}

我正在尝试将 <div>...</div> 传递到 Format 组件中,但出现错误:Type '{ children: Element[]; }' has no properties in common with type 'IntrinsicAttributes',关于这种情况我找不到太多相关信息。

1 个答案:

答案 0 :(得分:2)

找到答案,必须传入 props 作为参数,因为 this.props 指的是类 props 而不是函数 props。

    Format = (props) => {
        return(
            <div>
                {props.children}
            </div>
        )
    }