What is Props

We use the props (properties passing) system to properties to config the component

It is a 3-step process.

  1. Identify the variable or the value that we want to be provided by the parent

  2. Provide a reference to the props that’s coming in from the parent component. (i.e. Props Object)

Props is a Javascript Object

The first argument to the function:

const Header = props => {
  const { textStyle, viewStyle } = styles;
  return (
    <View style={viewStyle}>
      <Text style={textStyle}>{props.headerText}</Text>
    </View>
  );
};

Example

Header Component

// import libraries for making a component
import React from "react";
import { Text, View } from "react-native";

// Make a component
const Header = props => {
  const { textStyle, viewStyle } = styles;
  return (
    <View style={viewStyle}>
      <Text style={textStyle}>{props.headerText}</Text>
    </View>
  );
};

const styles = {
  //  return JSON Object, used to define the CSS Style
  textStyle: {
    // textStyle is the properties
    fontSize: 20
  },
  viewStyle: {
    backgroundColor: "#F8F8F8",
    justifyContent: "center",
    alignItems: "center",
    height: 60,
    paddingTop: 15,
    // Shadow Related config
    shadowColor: "#000",
    shadowOffset: {
      width: 0,
      height: 2
    },
    shadowOpacity: 0.2, // darkness of shadow
    //
    elevation: 2,
    position: "relative"
  }
};

// Make the component available to another parts of the app
export default Header;

App

Last updated

Was this helpful?