[英]React native styled components cannot override component's style
I have a weird problem with stlyed components.我对 stlyed 组件有一个奇怪的问题。 I have a component Header with a basic style but when a try to use this component and extend the style nothing happens.
我有一个具有基本样式的组件 Header,但是当尝试使用此组件并扩展样式时,没有任何反应。 Can someone tell me what going on?
有人能告诉我发生了什么事吗?
import styled from 'styled-components/native'; export const Container = styled.SafeAreaView``; export const Content = styled.View` height: 72px; padding: 0 24px; flex-direction: row; align-items: center; justify-content: center; `;
Header component import React, { PropsWithChildren, FC } from 'react'; import { Container, Content } from './styles'; const Header: FC = ({ children }: PropsWithChildren<unknown>, props) => { return ( <Container {...props}> <Content>{children}</Content> </Container> ); }; export default Header;
import styled from 'styled-components/native'; import Header from '../components/Header/index'; export const Container = styled(Header)` background: blue; height: 200px; `;
You have to pass your props from into your Header
component.您必须将您的道具从传递到您的
Header
组件。 In Container
or Content
.在
Container
或Content
中。 It's won't be done instead of you.它不会代替你完成。
Your Header
is a React component and he "doesn't know what to do" with props that it will receive from Container
- const Container = styled(Header)'...'
.您的
Header
是一个 React 组件,他“不知道如何处理”将从Container
接收的道具 - const Container = styled(Header)'...'
。 Props will be recognized correctly if component is working with styles, as Text
, View
, ...如果组件使用 styles,道具将被正确识别为
Text
, View
,...
export const Container = styled(Header)` background: blue; height: 200px; `; const Header: FC = ({ children, ...restProps }: PropsWithChildren<unknown>) => { return ( <Container {...restProps}> <Content>{children}</Content> // or <Content {...restProps}>... </Container> ); };
or you have 2 next options, without passing the props - just editing your inner Container.或者您有 2 个下一个选项,无需传递道具 - 只需编辑您的内部容器。 It's depends on your codestyle of the project
这取决于您的项目代码风格
const Header: FC = ({ children }: PropsWithChildren<unknown>) => { return ( <Container background="blue" height="200px"> <Content>{children}</Content> </Container> ); };
export const NewContainer = styled(Container)` background: blue; height: 200px; `; const Header: FC = ({ children }: PropsWithChildren<unknown>) => { return ( <NewContainer> <Content>{children}</Content> </NewContainer> ); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.