【发布时间】:2019-07-15 17:05:51
【问题描述】:
我有这样的组件
import React from 'react';
import styled from '../../styled-components';
const StyledInput = styled.input`
display: block;
padding: 5px 10px;
width: 50%;
border: none;
border-radius: 10px;
outline: none;
background: ${(props) => props.theme.color.background};
font-family: 'Roboto', sans-serif;
`
;
export const Input = () => {
return <StyledInput placeholder="All notes"></StyledInput>
}
我想像这样将它们作为“索引”组件的属性放置
import styled from 'styled-components';
import { Input } from './Input';
const NoteTags:any = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
width: 20%;
height: 5%;
border-bottom: 2px solid ${(props) => props.theme.color.background}; `;
NoteTags.Input = Input;
export default NoteTags;
那么我可以像这样使用它们
import React from 'react';
import NoteTags from '../../blocks/note-tags';
const ComponentNoteTags = () => {
return (
<React.Fragment>
<NoteTags.Input></NoteTags.Input>
</React.Fragment>
)
}
export default ComponentNoteTags;
问题在于NoteTags 上不存在属性Input,所以打字稿给我一个错误。我可以通过将类型 any 设置为 NoteTags 来解决它,但我不确定这是不是最好的方法。
有没有更好的解决方法?
【问题讨论】:
标签: reactjs typescript styled-components