对于react的三大核心属性state、props、refs,只有props能被函数式组件使用,因为只有props属性可以作为参数调用。

相关代码如下:

<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><title>3_函数组件使用props</title>
</head>
<body><!--准备好一个容器--><div id="test1"></div><div id="test2"></div><!--引入react核心库--><script type="text/javascript" src="../js/react.development.js"></script><!--引入react-dom,用于支持react操作DOM--><script type="text/javascript" src="../js/react-dom.development.js"></script><!--引入babel,用于将jsx转为js--><script type="text/javascript" src="../js/babel.min.js"></script><!--引入prop-types,用于将对组件标签属性进行限制--><script type="text/javascript" src="../js/prop-types.js"></script><script type="text/babel">   //<!--此处一定要写babel-->//创建组件function Person(props){const {name,gender,age} = propsreturn (<ul><li>姓名:{name}</li><li>性别:{gender}</li><li>年龄:{age}</li></ul>)}Person.propTypes={name:PropTypes.string.isRequired,     //name指定为字符串类型,而且必须得填,不能为空gender:PropTypes.string,age:PropTypes.number,speak:PropTypes.func,       //限制speak为函数}Person.defaultProps = {         //如果gender或者age值为空,就使用默认值gender:'男',age:18}//渲染组件到页面ReactDOM.render(<Person name="sandy" age={18} gender="女" speak={speak}/>,document.getElementById('test1'))const p={name:'小唐',age:18,gender:'女'}ReactDOM.render(<Person {...p}/>,document.getElementById('test2'))function speak(){console.log('happy');}</script></body></html>

React 之 函数式组件使用props相关推荐

  1. React之函数式组件使用props

    <!DOCTYPE html> <html> <head> <meat charset="UTF-8"> <title> ...

  2. react中函数式组件React Hooks

    React Hooks 函数式组件 使用hooks理由 高阶组件为了复用,导致代码层级复杂 生命周期的复杂 写成functional组件,无状态组件,因为需要状态,又改成了class,成本高 在16. ...

  3. [react] react的函数式组件有没有生命周期?

    [react] react的函数式组件有没有生命周期? 没有 个人简介 我是歌谣,欢迎和大家一起交流前后端知识.放弃很容易, 但坚持一定很酷.欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

  4. React之函数式组件和高阶组件(装饰器、带参装饰器)

    import React from 'react'; // 此句不能少 import ReactDom from 'react-dom';const Wrapper = props => < ...

  5. 前端学习(2224):react之函数式组件

    import React from 'react'; import ReactDOM from 'react-dom';function Childcom() {let title = < h2 ...

  6. React之函数式组件

    <!DOCTYPE html> <html> <head> <meat charset="UTF-8"> <title> ...

  7. 前端学习(3222):函数式组件使用props

  8. 2021-05-29 react antd 函数式组件 使用history.push(url)时,跳转不刷新的问题?

    **在传递URL使使用给传递了一个state成功解决问题 形如history.push(url,{a:1}) **

  9. reactjs组件实例的三大属性之props使用示例:在函数式组件中使用props

    props基本使用 <!DOCTYPE html> <html lang="en"> <head><meta charset=" ...

最新文章

  1. 分布式缓存Redis Centos下单节点安装
  2. 收藏 | 这100+个免费API,免了自己去爬的烦恼!
  3. AIM Tech Round 4 (Div. 2)ABCD
  4. UVA 10269 Super Mario,最短路+动态规划
  5. Oracle 常用sql场景应用(未完待续......)
  6. xilinx芯片管脚使用限制_修复焊接BGA芯片过程
  7. codeforces B. Pasha and String(贪心)
  8. GODIVA:只需一步,文字到视频秒级生成,微软亚洲研究院最新成果
  9. [转载] Python杂谈 | (6) numpy中array()和asarray()的区别
  10. phpcms V9 栏目管理
  11. python ----json数据处理
  12. ASP.NET 学习日志
  13. DC概论三之setup time 与 hold time 之二
  14. paip.mysql备份慢的解决
  15. html网页自动提示框代码,多种网页弹出窗口代码
  16. 论文解读|BMVC 2019 |Fast-SCNN Fast Semantic Segmentation Network
  17. linux下 OOB 炸弹的制作
  18. 谭浩强C++ 第十章
  19. 计算机上如何保存ico格式,PS怎么保存ico格式
  20. echarts自定义视图数据(dataView)

热门文章

  1. signature=4f0a1c9773fc07031e260b016c514c92,针对协同过滤推荐系统的推举托攻击研究
  2. 旧文新发之三----清明种种
  3. 前端工程化研究之npm私服
  4. linux之top命令详解
  5. Java解析rdf和xml文件以及处理excel
  6. 用Delphi写一个UTF8编码格式的文本文件
  7. MATLAB算法实战应用案例精讲-【深度学习】多尺度特征融合-图像识别(论文篇三)
  8. 利用.htaccess来禁止某IP访问
  9. mysql导入数据库报错解决方式
  10. 邮件不小心删除,怎么进行恢复呢?