简体   繁体   English

在课堂上反应js道具

[英]react js props in class

i am facing one problem with one simple code. 我用一个简单的代码面临一个问题。

i am having a props in my component tags called name and age. 我的组件标签中有一个道具,叫做名称和年龄。 And i want to use this inside my class Login 我想在班级内部使用它

my code look like this way 我的代码看起来像这样

export default class App extends Component {

//constructor(props){

//super(props); //}

render(){ return(

  <div className="person">
  <h1>{  }</h1>
  <p> Your age is 37</p>


  </div>

); } }

ReactDOM.render(<App name="john" age="37"/>, document.getElementById('root'));

i want to access the attribute of my component name and age. 我想访问组件名称和年龄的属性。 And render them in the above html. 并在上面的html中呈现它们。

You are passing the data to the App, but not actually using it in the JSX. 您正在将数据传递给App,但实际上并未在JSX中使用它。

export default class App extends Component {

  constructor(props) {
    super(props);
  }

  render() { 
    const {name, age} = this.props;
    return (
      <div>
        <h1>Hello {name}</h1>
        <p> Your age is {age}</p>
      </div>

    ); 
  } 
}

ReactDOM.render(<App name="john" age="37"/>, document.getElementById('root'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM