React中props

在React中props的使用场景挺多的,这里总结下

1. 组件的props

// 使用组件
<Hello name="Aaron" age={18}>
// 函数组件 使用props
function Hello(props) {
	return (
		<div>接收到数据: {props.name}</div>
	)
}
// 类组件 使用this.props
class Hello extends React.Component {
	render() {
		return (
			<div>几首到的数据{this.props.age}</div>	
		)
	}
}

2. 组件通讯

  • 父 => 子
// 父组件
class Parent extends React.Component {
	state = { lastName: '陈' }
    render() {
   	return (
   		<div>
   			传递数据给子组件: <Child name={this.state.lastName} />
   		</div>
   	)
   }
}
// 子组件
class Child extends React.Component {
   render() {
   	return (
   		<div>
   			父传给子的数据: {this.props.name}
   		</div>
   	)
   }
}
  • 子 => 父
// 父组件
class Parent extends React.Component {
	state = {
   	parentMsg: ''
   }
	getChildMsg = data => {
   	this.setState({
   		parentMsg: data
   	})
   }
   render() {
   	return(
   		<div>
   			父组件: {this.state.parentMsg}
   			<Child getMsg={this.getChildMsg} />
   		</div>	
   	)
   }
}
	// 子组件
	class Child extends React.Component {
   	state = {
   		msg: '学前端'
   	}
   	handleClick = () => {
   		this.props.getMsg(this.state.msg)
   	}
   	render() {
   		return (
   			<div>
   					子组件: <button onClick={this.handleClick} />
   				</div>
   		)
   	}
   }
  • 兄弟组件通讯(借助父组件)
class Counter extends React.Component {
   state = {
   	count: 0
   }
   onIncrement = () => {
   	this.setState({
     		count: this.state.count + 1
  		 })
 	}
 	render() {
   	return (
   		<div>
   			<Child1 count={this.state.count} />
   			<Child2 onIncrement={this.onIncrement} />
   		</div>
   	)
   }
}
// Child1
const Child1 = (props) => {
 	return <h1>计数器: {props.count}</h1>
}
const Child2 = (props) => {
 	return <button onClick={() => props.onIncrement()}>+1</button>
}
  • 对于通讯来说还可以使用Context解决祖孙组件的通讯
const { Provider, Consumer } = React.createContext()
class App extends React.Component {
 render() {
   return (
   // value表示要传递的数据
     <Provider value='pink'>
       <div className='app'>
         <Node />
       </div>
     </Provider>
     
   )
 }
}
const Node = props => {
 return (
   <div className="node">
     <SubNode />
   </div>
 )
}
const SubNode = props => {
 return (
   <div className='subnode'>
     <Child />
   </div>
 )
}
const Child = props => {
 return <div className='child'>
   <Consumer>
     {
       data => <span>我是子节点 -- {data}</span>
     }
   </Consumer>
 </div>
}

3.props校验

  • children属性
// 当组件中有子节点时, props.children就代表该子节点的内容
function Hello(props) {
	return (
		<div>
			组件的子节点: {props.children}
		</div>
	)
}
<Hello>我是子节点</hello>
  • props校验(由于使用组件的时候传递的数据格式可能不符合组件自身设定的格式,可以使用props校验)
    npm i props-types|yarn add prop-types
import PropTypes from 'prop-types'
function App(props) {
	return (
		<h1>Hi,{props.colors}</h1>
	)
}
// 添加props校验
App.propTypes = {
  	colors: PropTypes.array
}
  • 其他约束
App.propTypes = {
  a: PropTypes.number,
  fn: PropTypes.func.isRequired,
  tag: PropTypes.element,
  // shape方法指定特定结构
  filter: PropTypes.shape({
    area: PropTypes.string,
    price: PropTypes.number
  })
}
  • props默认值
const App = props => {
  console.log(props)
  return (
    <div>
      <h1>此处展示props的默认值: {props.pageSize}</h1>
    </div>
  )
}

// 添加props默认值
App.defaultProps = {
  pageSize: 10
}

const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App pageSize={20} />);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值