react组件传值(props[只读属性]) 函数组件

组件间传值,在React中是通过只读属性 props 来完成数据传递的。

props:接受任意的入参,并返回用于描述页面展示内容的 React 元素。

function Cmp1(props) {

    return (

      <div>

        <h3>{ props.name } -- 你好世界</h3>

      </div>

    )

}

父组件

  
// react根组件
import React, { Component } from 'react'

// 导入
 import Items from './components/Items'
render() {
    return (
      <div>
        <Items title="我父组件给你的信息" num={100} bool={true} /> 
      </div>
    )
  }

子组件

import React from 'react';

// props是一个只读属性,不能进行数据修改const Items = (props) => {
  console.log(props)
  return (
    <div>
      我是一个函数组件
      <hr />
      <h3>{props.title}</h3>
    </div>
  );
} 

/* const Items = (props) => {
  let {title} = props
  return (
    <div>
      我是一个函数组件
      <hr />
      <h3>{title}</h3>
    </div>
  );
} */
export default Items;
 
右侧打赏一下 代码改变世界一块二块也是爱
原文地址:https://www.cnblogs.com/ht955/p/14667050.html