好客租房49-组件的props(特点)

特点

1可以给组件传递任意类型的数据

2props是只读的对象 只能读取属性的值 无法修改对象

3注意:使用类组件时 如果写了构造函数 应该将props传递给super() 否则 无法在构造函数

中获取到props

//导入react
import React from 'react'
import ReactDOM from 'react-dom'

//导入组件
// 约定1:类组件必须以大写字母开头
// 约定2:类组件应该继承react.component父类 从中可以使用父类的方法和属性
// 约定3:组件必须提供render方法
// 约定4:render方法必须有返回值

class HelloWorld extends React.Component {
	render() {
		console.log(this.props)
		return (
			<div>
				<h1>props:{this.props.name}</h1>
			</div>
		)
	}
}
//函数组件
// const HelloWorld = (props) => {
// 	console.log(props)
// 	return (
// 		<div>
// 			<h1>props:{props.name}</h1>
// 		</div>
// 	)
// }

ReactDOM.render(
	<HelloWorld name="geyao" color={['red', 'blue']}
    tags={<p>哈哈哈</p>} />,
	document.getElementById('root')
)
//导入react
import React from 'react'
import ReactDOM from 'react-dom'

//导入组件
// 约定1:类组件必须以大写字母开头
// 约定2:类组件应该继承react.component父类 从中可以使用父类的方法和属性
// 约定3:组件必须提供render方法
// 约定4:render方法必须有返回值

class HelloWorld extends React.Component {
    constructor(props){
        super(props)
        //constructor中拿不到props
        console.log(props)
    }
	render() {
		console.log(this.props)
		return (
			<div>
				<h1>props:{this.props.name}</h1>
			</div>
		)
	}
}
//函数组件
// const HelloWorld = (props) => {
// 	console.log(props)
// 	return (
// 		<div>
// 			<h1>props:{props.name}</h1>
// 		</div>
// 	)
// }

ReactDOM.render(
	<HelloWorld name="geyao" color={['red', 'blue']}
    tags={<p>哈哈哈</p>} />,
	document.getElementById('root')
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端大歌谣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值