样式化组件 - 如何防止道具传递给扩展组件?

时间:2018-04-28 10:03:51

标签: reactjs styled-components

我正在延长Textarea,我希望completed访问StyledItemTextarea,而不是Textarea。我怎样才能做到这一点?

import Textarea from 'react-textarea-autosize';

const TextareaAutosizeSC = styled(Textarea)`
    ...
`;

const StyledItemTextarea = TextareaAutosizeSC.extend`
    color: ${({ completed }) => completed ? '#ccc' : '#fff'};
`;

const MyTextarea = ({ completed }) => <StyledItemTextarea completed={completed} />;

2 个答案:

答案 0 :(得分:2)

也许是这样的:

const TextareaAutosizeSC = ({ completed, ...rest }) => {
  const TextArea = styled(Textarea)`
    ...
  `;

  return <TextArea {...rest} />
}

const TextareaAutosizeSC = styled(Textarea)`
    ...
`;

const TextAreaWithoutSomeProps = ({ completed, ...rest }) => <TextareaAutosizeSC {...rest} />

答案 1 :(得分:1)

我找到了解决方法:

import Textarea from 'react-textarea-autosize';

const StyledItemTextarea = styled(({ completed, ...rest }) => <Textarea {...rest} />)`
    color: ${({ completed }) => completed ? '#ccc' : '#fff'};
`;

const MyTextarea = ({ completed }) => <StyledItemTextarea completed={completed} />;