Can not apply styles to custom components in React Native using styled-components?

Musayyab Naveed

I'm using styled-components in my React Native app to style it. Styling works fine on other components but when I try to style my custom created components then no style is applied.

In this case, no padding is applied.

Here is one of my code example:

import React, { Component } from "react";
import { View } from "react-native";
import styled from "styled-components/native";
import { BigTitle } from "./Titles";

class Home extends Component {
  render() {
    return (
      <View>
        <MainTitle title="Hello World!" />
      </View>
    );
  }
}
export default Home;

const MainTitle = styled(BigTitle)`
  padding-top: 45px;
  padding-bottom: 10px;
`;


Here is the custom created component:


import React from "react";
import { Text } from "react-native";
import styled from "styled-components/native";

interface TitleProps {
  title: string;
}

export const BigTitle = (props: TitleProps) => {
  return <BigTitleText>{props.title}</BigTitleText>;
};

const BigTitleText = styled(Text)`
  text-align: left;
  font-size: 20px;
  letter-spacing: 1.8px;
  color: ${props => props.theme.textColor};
  font-family: ${props => props.theme.fontFamily};
  text-transform: uppercase;
  opacity: 1;
`;

Luan Eduardo

Try to use the style prop in the BigTitle component:

export const BigTitle = (props: TitleProps) => {
  return <BigTitleText style={props.style}>{props.title}</BigTitleText>;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to apply styles to multiple components using styled components

React styled-components not applying styles to components

Dynamically Styled Button in React Native using Styled Components

Overwriting nested styles in React Bootstrap using Styled-Components

How to do overlapping styles with Styled Components React?

Overriding react components styles with styled component

Using props in React with styled components across components

How to create shared styles using styled components?

Styled components not applying styles

How can I use custom icon in react-styled components?

Using Animated with styled-components (react-native)

Error when using calc() with react-native and styled-components

React Native "Styled-components" not working

React Native - Styling buttons with styled-components

How to apply local styles to imported React components

Where to put styled components styles

shorthand for custom components in react native?

React app with styled components and twin.macro set global styles

Share the same styles between two types of component with React Styled Components

React: Why not just use styles object instead of styled-components?

React-Router not correctly rendering Styled-Components Styles

Using props inside react styled components

Using CSS attr() with styled components in react Hooks

How to style react styled components using classNames

React Native Components Override styles "leaking"

Can I set styles to document body from styled-components?

Using styled-components to style my own React components

On styling of class components using React styled-components

Setup for React styled components