Styling custom component in react native

styled-components react native
react native stylesheet
react native styling best practices
react native styled-components box-shadow
react-native-web styled-components
react native elements card styling
react native stylesheet vs styled-components
react native elements styled components

I am trying to add styling to my custom component in react native, but no matter what I do, the style has no effect. Here is my code:

// App.js
import MyCustomComponent from './components/myCustomComponent.js';

render() {
  return (
    <View style={styles.container}>
      <MyCustomComponent style={{marginTop: 10}}/>
    </View>
  );
}

The project compiles fine, and my custom component appears on screen fine, but the marginTop styling is not applied. It is worth noting that the style for the parent View component does apply correctly. This is a brand new project I just created today. This seems like it should be extremely basic, but just isn't working. What can I do to apply this styling?

Custom component code:

import React, {Component} from 'react';
import {TextInput, StyleSheet, Image, View, Button} from 'react-native';

type Props = {};
export default class MyCustomComponent extends Component<Props> {

  render() {
    return (
      <View style={styles.container}>
        <Image
          source={{ uri: "source here" }}
          style={{ width: 50, height: 50 }}
         />
         <TextInput
          style={{ height: 50 }}
          placeholder="Search"
        />
      </View>
    )
  }
}

you can use this code:

export default class MyCustomComponent extends Component<Props> {
  render() {
    return (
      <View style={[styles.container, {...this.props.style}]}>
        ...
      </View>
    )
  }
}

now, styles.container is applied and enything you pass to component through style will be added to component style.

i hope this can help you

Styling Custom Components in React Native Using TypeScript, Learn how to use a styled-components in a React Native app by building a are going to be custom using semantics from styled-components . Styled components are a CSS-in-JS tool that bridges the gap between components and styling, offering numerous features to get you up and running in styling components in a functional and reusable way. In this article, you’ll learn the basics of styled components and how to properly apply them to your React applications.

You can apply a style to your custom component by passing style as props.

and

Use it as style={this.props.style} in your MyCustomComponent.

import React, {Component} from 'react';
import {TextInput, StyleSheet, Image, View, Button} from 'react-native';

type Props = {};
export default class MyCustomComponent extends Component<Props> {

  render() {
    return (
      <View style={[styles.container,{...this.props.style}]}>//<--Use like this---
        <Image
          source={{ uri: "source here" }}
          style={{ width: 50, height: 50 }}
         />
         <TextInput
          style={{ height: 50 }}
          placeholder="Search"
        />
      </View>
    )
  }
}

Using Styled Components with React Native | by Aman Mittal, ways of styling React Native Apps — StyleSheet and styled-components. Custom Components using StyleSheet vs styled-components. CSS or SCSS Stylesheets is a styling strategy that involves the use of external CSS or SASS stylesheets that can be imported into your React components depending on where you need the styling to be applied. For example, we have a SASS file of styles called Box.scss we need to use in a component called Box.js, below is the code for our SASS file.

For example, lets change background color of custom card. Custom Card:

export default function MyCard({color}) {
    return (
         <View style={[styles.card, {backgroundColor: color}]}>
        </View>
    )
}

In another file

<MyCard color={"pink"} />

Here, styles.card is the style added in Custom Card file and the color is given during component use. Note: MyCard({color}) if you miss to add highlight parentheses, it will not work. I faced this issue.

Understanding styling in React Native | by Yashish Dua, import React from 'react'; import { View, StyleSheet, Text } from 'react-native'; const Component = () => { return ( <View> <Text>This is a custom� Styling Custom Components in React Native Using TypeScript I recently ran into an issue with providing style properties to custom React Native components written in TypeScript. When defining the type of the style property, we were using the ViewStyle and TextStyle types provided by React Native.

You need to apply this style yourself inside MyCystomComponent. For example:

const MyCustomComponent = ({style}) => (
  <View style={style}> // This will be the style that is passed as a prop.
  </View>
);

Style is not passed down to custom components � Issue #10031 , These new elements are going to be custom using semantics from styled- components. export default class App extends React.Component {� One of the nice things about styling in React Native is the added styles to margin and padding. Both of them come with a way to set top and bottom in one line, as well as left and right in one line. Both of them use the words, Horizontal and Vertical.

Using Styled Components with React Native - DEV, With React Native, you style your application using JavaScript. All of the core components accept a prop named `style`. The style names and� STEP 1: Create a class for Custom component and define the props which will customize our Custom component. export STEP 2: Then let’s start with the render () method import React, { Component } from 'react'; import { View, Platform, STEP 3: Import the Custom Text Field Component in parent

Style � React Native, You can add styling to your component using style props you simply add style props to your element it accepts an object of properties. React Native provides other solutions like TouchableOpacity which allows you to create your own custom buttons and styles them however you like. You can create your custom button using <TouchableOpacity> or simply use it to apply some styles to the existing button. In the <ArticleItem> component, wrap both buttons as follows:

Styling in React Native. Explore the best ways to style a React…, Styled components are a CSS-in-JS tool that bridges the gap between check out the React Native guide, our focus here will be on styled components for React . Also, we used CSS custom properties because we can! The styled-components library enables you to write native CSS for styling a React Native component. Under the hood, styled-components simply converts the CSS text into a React Native StyleSheet object. Run the following command to install styled-components.

Comments
  • can you show me code of custom component?
  • style your MyCustomComponent to container view