简体   繁体   English

将道具传递给静态React组件

[英]passing props to a static React Components

I have created a static method in a react Class component when I pass props it does not seem to me that they are passed, however, the children prop for passed without any problem component calling 当我传递道具时,我已经在react Class组件中创建了一个静态方法,在我看来,它们似乎没有被传递,但是,传递的子道具没有任何问题组件的调用

This is the component and the given props when consoling I receive undefined !! 这是组件和安慰时给定的道具,我收到未定义的! The Component 组件

class Tabel extends Component {
static Head = ({ children, ...props }) => (
    <span className="table-head">
        {children}
    </span>
)

static Body = ({ children, ...props }) => (
    <div className="tabel-body">{children}</div>
)

render = () => (
    <div className='tabel-container' >
        {this.props.children}
    </div>
)

}

I am passing props like this 我正在传递这样的道具

<Tabel.Cell
  mainContent='$10 175.00'
  subContent={{ content: '12.4%', classes: 'stock-up' }}
/>

you can spread the props and it should not be an issue: 您可以散布道具,这应该不是问题:

class Tabel extends Component {
static Head = ({ children, ...props }) => (
    <span className="table-head"  {...props}>
        {children}
    </span>
)

static Body = ({ children, ...props }) => (
    <div className="tabel-body" {...props} >{children}</div>
)

render = () => (
    <div className='tabel-container' >
        {this.props.children}
    </div>
)

}

May be try like this 可以这样尝试

static Head = props => (
    <span className="table-head">
        {props.children}
        {props.prop1Name}
        {props.prop2Name}
    </span>
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM