Pass props from Parent to child component

Taras Danylchenko

I hava a Parent class

import React, { Component } from 'react';
import { View, TouchableOpacity, Text, ListView } from 'react-native';
import Profile from './UserBlock';    

export default class ControlPanel extends Component {
      constructor(props){
        super(props)
        console.log(this.props)
        this.state = {
          email: "[email protected]",
          first_name: "User",
          image : '../img/user.png'
        }
      }

      render() {
        return(
        <View style={{backgroundColor:'rgba(255,255,255,0.93)', flex:1,}}>
          <Profile {...this.props} />
        </View>)
      }
    }

And child component

import React, { Component } from 'react';
import { View, Text, Image } from 'react-native';

    export default class UserBlock extends Component {
              constructor(props){
                super(props)
                this.state = {}
              }

              render() {
                return(
                <View style={{flexDirection:'row', padding:10}}>
                    <Image source ={{uri : this.props.state.image}} resizeMode="contain" style={{margin:15, width:60, height:60, borderRadius:30}} /> 
                    <View style ={{justifyContent:'center', margin:15}}>
                    <Text style={{fontWeight:'700', fontSize:25, color:'#444'}}>{this.props.state.first_name}</Text>
                    <Text style={{fontWeight:'200', color:'#999'}}>{this.props.state.email}</Text>
                    </View>
                </View>)
              }
            }

But when im trying to read parent props i have a error "TypeError: undefined is not an object"
But it seemed to me that I was doing everything right.

Karl Taylor

You are not passing the props down to the child component correctly.

What you are doing is using the spread operator to pass down multiple key/values, but if your props are empty, then nothing will get passed.

<Profile {...this.state} />

is the same as

<Profile
  email={this.state.email}
  first_name={this.state.first_name}
  image={this.state.image}
/>

To get the parent state into the child component you need to do:

<Profile {...this.state} />

Then in your child component

console.log(this.props) // would log:
// email: "[email protected]"
// first_name: "User"
// image : '../img/user.png'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unable to pass props from parent to child and save it in state of child component

How to pass props from child component to its parent of parent?

Unable to pass props to child Component from Parent Component

VueJS - unable to pass props from parent to child component

In React, is it a good practice to pass all the props from parent to child component?

How to pass multiple props from parent to child component in Vue

Pass props from parent to child component that uses hooks?

React: How to pass function as props from Functional Parent component to child

How to pass props from child component to parent component to another child component in ReactJS?

How to call Child function from Parent / Pass Props to a Parent component without using ref methods?

React router dom passing data from parent component to child router component does not pass the props.match

I receive an undefined value when I pass the value of a variable through props from a parent component to a child component

How to pass an array from a parent component to child component using props in React Native?

How can I pass image url from parent component to child component via props [REACT.JS]

How to pass axios response object from a parent component to child component using props in vue

Change parent component props/state from child

Passing props from parent component to child components

React, pass data from child to parent with props

Pass props from child to parent react navigation

How to pass props from child to parent?

Vuejs - pass data from parent to child with props

How to pass props from parent to child in react?

How to pass props to parent from child in React?

Pass state from child component to parent component

Pass variable from parent component to child component

pass object from state as props to child component

Pass Values from JSON as Props to the child component

Pass props with callback from Parent to component

Pass props from parent to component in Const