简体   繁体   English

将组件放入react-emotion或styled-component中

[英]get component inside react-emotion or styled-component

import styled from 'react-emotion'

class Field extends React.Component<> {}

export default styled(Field)

then if I render this component and use component.type , i get styled()... function instead of Field component. 然后如果我渲染这个组件并使用component.type ,我得到styled()...函数而不是Field组件。

How I get Field inside styled function? 我如何在样式函数中获得Field

Try this: 尝试这个:

import styled from 'react-emotion'

const Field = ({ className }) => (
    <div className={className}>Some field</div>
)

const theField = styled(Field)`
    color: green;
`
export default theField 

Exporting the function is just exporting the function, but I think you want the component. 导出函数只是导出函数,但我认为你想要组件。 So why not this? 那为什么不呢?

import styled from 'react-emotion'

class Field extends React.Component<> {}

const styledField = styled(Field)

export default styledField

styled library doesn't provide any way to get a Field. styled库没有提供任何获得Field的方法。 Even if it does Field.type will be undefined since Field doesn't have this property. 即使它确实Field.type将是undefined因为Field没有此属性。 You can try this code and see: 您可以尝试此代码并查看:

 class Field extends React.Component<> {
  render() {
    return (<div></div>);
  }
}

console.log(Field.type); // this will be 'undefined' not 'div' as you might expect

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

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