3
import React from 'react';
import ReactDOM from 'react-dom';

class Parent extends React.Component {
    let name=this.props.name;

    render() {
       return (
            <h1>
              {name}
           </h1>
      );
   }
}

ReactDOM.render(
  <Parent name="Luffy"/>,
  document.getElementById('app')
);e

Hi All,I'm new to React and i'm stuck in a problem,i wanted to display the name through props but its not working, if i use name=this.props.name inside render() it works fine,But how to get its value outside render,Please help out and Thanks in advance

2

2 Answers 2

6

According to the ES wiki

There is (intentionally) no direct declarative way to define either prototype data properties (other than methods) class properties, or instance property

Class properties and prototype data properties need be created outside the declaration.

Properties specified in a class definition are assigned the same attributes as if they appeared in an object literal.

a class definition defines prototype methods - defining variables on the prototype is generally not something you do.

To get the value outside of render, you can have a variable in the constructor and then access its value like

class Parent extends React.Component {
    constructor(props) {
       super(props);
       this.name = props.name
    }
    render() {
       return (
            <h1>
              {this.name}
           </h1>
      );
   }
}

ReactDOM.render(
  <Parent name="Luffy"/>,
  document.getElementById('app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app'></div>

With the Es7 initializers. you can do

class Parent extends React.Component {
    name = this.props.name
    render() {
       return (
            <h1>
              {this.name}
           </h1>
      );
   }
}

ReactDOM.render(
  <Parent name="Luffy"/>,
  document.getElementById('app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app'></div>

However, since you are assigning a value based on the props, the ideal method is to make use of lifecycle functions and then use it. If you want to update in a state, then the better palce is to have this logic in the componentWillMount and the componentWillReceiveProps fucntion.

However if you only want to update the variable and use it in render, the best place is to have it in the render function itself

class Parent extends React.Component {
    state = {
        name: ''
    }
    componentWillMount() {
      this.setState({name:this.props.name});
    }
    componentWillReceiveProps(nextProps) {
        this.setState({name: this.props.name});
    }
    render() {
       return (
            <h1>
              {this.state.name}
           </h1>
      );
   }
}

ReactDOM.render(
  <Parent name="Luffy"/>,
  document.getElementById('app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app'></div>

or

class Parent extends React.Component {
   
    render() {
       let name = this.props.name
       return (
            <h1>
              {name}
           </h1>
      );
   }
}

ReactDOM.render(
  <Parent name="Luffy"/>,
  document.getElementById('app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app'></div>

Sign up to request clarification or add additional context in comments.

3 Comments

OP isn't updating a value based on props, they're setting a value based on props. I don't see any indication it's expected to update (and obviously if they are, they'll be sad).
The final snippet is a repeat of what the OP already knows works. The question is what problem they're trying to solve--without knowing that, providing meaningful advice is difficult.
@DaveNewton, As I mentioned in the answer, what other person is trying to achieve can be done by using ES7 style or using the constructor. And I agree it is difficult to advice what the OP wants to achive, however I gave him hte possible approaches
0
import React from 'react';
import ReactDOM from 'react-dom';

class Parent extends React.Component {
    let name=props.name; // no need to use this.props.name
    /* but a better way is to directly use {this.props.name} 
       inside the <h1> tag if you are not manipulating the data.
    */
    render() {
       return (
            <h1>
              {name}
           </h1>
      );
   }
}

ReactDOM.render(
  <Parent name="Luffy"/>,
  document.getElementById('app')
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.