React中是通过props来传递数据的
父组件给子组件传递数据,直接用属性名传递,子组件通过props获取父组件传递过来的值
//父组件 import Child from '../Child/index' const Parent = () => {
return( <div> <Child title='测试组件' /> </div> ) } export default Parent
//子组件 const Child = (props) => {
console.log(props.title,'title') return( <div> 子组件 </div> ) } export default Child
//父组件 import Child from '../Child/index' const Parent = () => {
const onClick = (value) => {
console.log(value,'点击了') } return( <div style={
{
background:'#fff',height:'500px',width:'500px'}}> <Child title='测试组件' click={
onClick} /> </div> ) } export default Parent
//子组件 const Child = (props) => {
console.log(props.title,'title') const handleClick = (value) => {
props.click(value) } return( <div style={
{
background:'red',height:'200px',width:'200px'}} onClick={
()=>{
handleClick(1)}}> 子组件 </div> ) } export default Child
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212685.html原文链接:https://javaforall.net
