admin管理员组

文章数量:1325408

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

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

Share Improve this question edited Jul 12, 2017 at 6:22 Shubham Khatri 282k58 gold badges431 silver badges411 bronze badges asked Jul 12, 2017 at 5:10 AbhishekAbhishek 3511 gold badge3 silver badges18 bronze badges 2
  • 2 Please go through react ponent life cycle.facebook.github.io/react/docs/react-ponent.html – Pardeep Dhingra Commented Jul 12, 2017 at 5:14
  • Why do you want to do it like this? What actual problem are you trying to solve? – Dave Newton Commented Jul 12, 2017 at 5:14
Add a ment  | 

2 Answers 2

Reset to default 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./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./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./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./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 ponentWillMount and the ponentWillReceiveProps 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: ''
    }
    ponentWillMount() {
      this.setState({name:this.props.name});
    }
    ponentWillReceiveProps(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./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./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./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app'></div>

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')
);

本文标签: javascriptSpecifying variable outside of render in React classStack Overflow