首页 > 其他分享 >react三大核心之一props

react三大核心之一props

时间:2022-08-22 22:45:16浏览次数:82  
标签:render react props 组件 document 三大 属性

-

html标签可以在标签上写自定义属性,那么react的组件,也可以像传属性一项,给组件传props;

react组件接收到传入的属性后,会自动塞进实例的props属性中,通过this.props可以拿到外部传入的属性

来看一下props的基本使用

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>组件实例核心属性1:state</title>
</head>
<body>
  <!-- 准备一个容器 -->
  <div id="test"></div>
  <div id="test2"></div>
  <div id="test3"></div>
  <!-- 引入react核心库 -->
  <script src="https://cdn.bootcdn.net/ajax/libs/react/18.2.0/umd/react.development.min.js"></script>
  <!-- 引入react-dom, 用于支持react操作dom, 需要在核心库之后引入 -->
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/18.2.0/umd/react-dom.development.min.js"></script>
  <!-- 引入babel.js,用于将jsx转化为js -->
  <script src="https://cdn.bootcdn.net/ajax/libs/babel-standalone/7.18.7/babel.min.js"></script>

  <script type="text/babel">
    // 1.创建类组件
    class Person extends React.Component{
      render() {
        console.log(this);
        const { name, age, sex } = this.props;
        return (
          <ul>
            <li>姓名: {name}</li>
            <li>性别: {sex}</li>
            <li>年龄: {age}</li>
          </ul>
        )
      }
    } 
    // 挂载
    ReactDOM.render(<Person name="tom" age="18" sex="女" />, document.getElementById('test'))
    ReactDOM.render(<Person name="老刘" age="30" sex="男" />, document.getElementById('test2'))
    ReactDOM.render(<Person name="老王" age="19" sex="女" />, document.getElementById('test3'))
  </script>
</body>
</html>

 

 

 

-

标签:render,react,props,组件,document,三大,属性
From: https://www.cnblogs.com/fqh123/p/16614508.html

相关文章

  • React报错之React Hook 'useEffect' is called in function
    正文从这开始~总览为了解决错误"ReactHook'useEffect'iscalledinfunctionthatisneitheraReactfunctioncomponentnoracustomReactHookfunction",可以将......
  • 实现最简单的 React DOM Diff 算法
    实现最简单的ReactDOMDiff算法本文写于2022年08月22日定义VNode与VNodeList类型首先我们定义一个简单的VNode类型。typeFlag="Placement"|"Delet......
  • 手写实现react hooks
    实现一些简单的reacthooks........在钩子函数中不要使用if判断,避免钩子错乱建立数组映射,建立多组钩子初始化数组和索引,全局使用lethookIndex=0lethookState=[......
  • 非props的属性,那么该属性会添加到子元素的根元素上 , 在子元素想到得到这些属性可以
    父组件:<template><!--如果当前的属性是一个非prop的attribute,那么该属性会默认添加到子组件的根元素上--><show-infoname="why":age="18":height="1.88......
  • props的对象写法
    父组件:<template><!--1.展示why的个人信息--><show-infoname="why":age="18":height="1.88"/></template><script>importShowInfofrom'./ShowI......
  • react面试题
    react事件机制在得到dom树之后,react会处理属性上是否有事件,react不会把事件绑定到真正的节点上,而是把所有的事件绑定在document(最外层节点)上,部分事件除外,如audio、video的......
  • React 源码-React 事件全解
    事件系统reactv17事件绑定事件绑定在函数setInitialDOMPropertiessetInitialDOMProperties将在complete阶段执行functionsetInitialDOMProperties(tag:st......
  • vscode中react标签自动补全
    在vscode中编写react时,发现标签没有自动补全,写起来不太灵活,查资料发现:默认在js文件中JSX语法无法自动补全,解决方法如下:文件=》首选项=》设置搜索"emmet.includeLanguages"......
  • react组件三大核心之一state
    -<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="wi......
  • Hadoop及其三大组件原理
    Hadoop是什么?由Apache基金会开发的分布式系统基础架构海量数据的存储和分析计算 Hadoop架构历史:1.0HDFS和MapReduce2.0在1.0基础上增加了YARN(任务调度),解放了Ma......