4

I have two components.

  1. MypropDestructuring.jsx (main component)
  2. MypropDestructuringMessage.jsx (using for prop destructuring) (I'm new for destructuring concept in React)

I think I am doing somewhere wrong in (MypropDestructuringMessage.jsx) props destructuring.

On console log I am getting following error-

Uncaught ReferenceError: props is not defined at MypropDestructuringMessage.render (index.js:33930)

Code:

MypropDestructuring.jsx

// Let's import react
import React from "react";

// Import custom component
import MypropDestructuringMessage from "./MypropDestructuringMessage.jsx";


// Let's create component [[ MypropDestructuring ]]
class MypropDestructuring extends React.Component{

    // Let's create custom method
    _myProfile() {

        const myProfileData = [
            {
                id : 1,
                name : "Neon",
                age : 25,
                location : "UK",
                skill : "UI Dev"
            }
        ];

        // return
        return myProfileData.map( (profileArrg) => {

                return( 
                    <MypropDestructuringMessage key={profileArrg.id} name={profileArrg.name} age={profileArrg.age} location={profileArrg.location} skill={profileArrg.skill} /> 
                );
            }
        );

    }

    render(){

        const showProfile = this._myProfile();
        return(
            <section>

                <p>&nbsp;</p>
                <h6> Prop Desturcturing </h6>
                <hr />

                <div>
                    {showProfile}
                </div>

            </section>
        );
    }
}


// Let's render ReactDOM
export default MypropDestructuring;

MypropDestructuringMessage.jsx

// Let's import react
    import React from "react";

    // Let's create component [[MypropDestructuringMessage]]
    class MypropDestructuringMessage extends React.Component{
        render(){


            // Props destructuring
            const {name, age, location, skill} = props;

            return(

                <section>

                    <div>
                        <ul className="list-group">
                            <li className="list-group-item"> {name} </li>   
                            <li className="list-group-item"> {age} </li>   
                            <li className="list-group-item"> {location} </li>   
                            <li className="list-group-item"> {skill} </li>    
                        </ul>
                    </div>

                </section>
            );
        }
    }

    // Let's pass data with [[ propTypes ]] - object
    MypropDestructuringMessage.propTypes = {
        name : React.PropTypes.string.isRequired,
        age : React.PropTypes.number.isRequired,
        location : React.PropTypes.string.isRequired,
        skill : React.PropTypes.string.isRequired
    }


    // Let's render ReactDOM
    export default MypropDestructuringMessage;
Pranjal Bikash Das
  • 1,092
  • 9
  • 27
Sandy
  • 325
  • 1
  • 9
  • 18

2 Answers2

7

Use this.props not only props :

const {name, age, location, skill} = this.props;

Here is the documentation on Destructuring assignment

Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
  • Thanks Vivek, I am new for understanding destructuring. Can you share some document with examples on destructuring? – Sandy Nov 06 '17 at 07:25
  • @Sandy, sure , updated the answer , please have a look. – Vivek Doshi Nov 06 '17 at 07:32
  • @Sandy , you might wanna read this too https://stackoverflow.com/questions/44734548/what-is-the-children-prop-in-react-component-and-what-proptypes-do/44734573#44734573 – Shubham Khatri Nov 06 '17 at 07:34
3

Props are bound to context of the component (this), they are not global, that's why you're getting an error. Use this.props instead of props, unless you need to handle them in constructor, as in constructor this.props is still undefined. If you need props in constructor, they are sent as a parameter:

constructor(props) {
    super(props);
    // this.props is not defined at this point, so you have to use props
    // do something with props here
}

You can read more on props in react in this article, it's useful for people new to React or ones who want to freshen up their knowledge, so check it out if you're interested!

Nesha Zoric
  • 6,218
  • 42
  • 34