美文网首页
props参数传入问题

props参数传入问题

作者: J_wendel | 来源:发表于2018-04-07 16:24 被阅读290次

props参数传入:

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

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

测试模板(正常运行)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!--/*1. 选择使用CDN*/-->
    <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
    <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
    <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>

    <title>React 学习</title>
</head>
<body>
<div id="dd">

</div>
<script type="text/babel">
    var divdd = document.getElementById('dd');

  //测试函数组件
    function Welcome(props) {
        return <h1>Hello, {props.name}</h1>;
    }

  //测试class组件
    class NewWelcome extends React.Component{
        render(){
            return <h1>Hello, {this.props.name}</h1>;
        }
    }

    ReactDOM.render(
            <Welcome name="wendel"/>,
        divdd
    );

</script>

</body>
</html>

result


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

    function Welcome() {
        return <h1>Hello, {props.name}</h1>;
    }

// or 下面这种写法
    function Welcome() {
        return <h1>Hello, {this.props.name}</h1>;
    }

结果直接报错了.


二, 测试class组件

  //测试class组件
    class NewWelcome extends React.Component{
        render(){
            return <h1>Hello, {this.props.name}</h1>;
        }
    }

运行结果ok

去掉this

  //测试class组件
    class NewWelcome extends React.Component{
        render(){
            return <h1>Hello, {props.name}</h1>;
        }
    }

报错 Uncaught ReferenceError: props is not defined

相关文章

  • props参数传入问题

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

  • Vue组件的属性验证

    在组件中的props传入的参数验证 格式

  • 封装组件的注意事项

    数据从父组件传入 props属性中添加验证规则: 通过props传入的参数不建议对其进行操作,会同时修改父组件中的...

  • Vue - 父子组件传值

    父组件向子组件传值 在子组件的props中声明想要接受的参数,父组件在使用子组件时传入参数。 使用props传值时...

  • react第4天

    props 子组件 需要传入两个参数person size父组件使用到子组件在组件使用中传入 person siz...

  • Vue列表多想选中和单向选中效果

    问题:Vue项目会遇到一些选中高亮效果,将选项列表写入组件,需要让组件即支持单选也支持多选,通过传入props参数...

  • subscriptions订阅

    前言 subscriptions:可以理解为监听事件。订阅方法传入的props参数,为dispatch和histo...

  • react--props

    props属性: props对于模块来说属性外来属性 传递参数: 模块中接受参数:this.props.username

  • React属性(props)和状态(state)

    React属性(props)和状态(state) 一、属性(props) 属性props是由外部传入、描述性质的,...

  • javascript函数

    函数 1.JavaScript允许传入任意个参数而不影响调用,因此传入的参数比定义的参数多也没有问题,虽然函数内部...

网友评论

      本文标题:props参数传入问题

      本文链接:https://www.haomeiwen.com/subject/khyyhftx.html