props参数传入问题

props参数传入:

方法组件, 需要传入props 参数, 方法内部直接使用props.(不需要this),
class组件, 不需要传入props 参数, 方法内使用this.props.

官网介绍
(https://www.reactjscn.com/docs/components-and-props.html)

测试模板(正常运行)




    
    
    
    
    

    React 学习


result


一, 测试函数组件, 去掉props参数传递

    function Welcome() {
        return 

Hello, {props.name}

; } // or 下面这种写法 function Welcome() { return

Hello, {this.props.name}

; }

结果直接报错了.


二, 测试class组件

  //测试class组件
    class NewWelcome extends React.Component{
        render(){
            return 

Hello, {this.props.name}

; } }

运行结果ok

去掉this

  //测试class组件
    class NewWelcome extends React.Component{
        render(){
            return 

Hello, {props.name}

; } }

报错 Uncaught ReferenceError: props is not defined

你可能感兴趣的:(props参数传入问题)