除了在元件标签上设定属性传入props外,还有props.children这种方法
透过在包在元件的开始、结束标签中间的Children传入,让子元件以props.children接收资料状态。
父元件
在Child元件标签中间插入"Text From Children"字串
import React from 'react'
import './All.css';
import Child from './Child';
function Father() {
return (
<div className="container">
<Child >
Text From Children
</Child>
</div>
)};
export default Father;
子元件
Child元件接收的参数props
import React from 'react'
import './All.css';
function Child(props) {
console.log(props)
return (
<div>{props.children}</div>
)}
export default Child;
用console.log看一下props的资料型态是如何
props物件中,传递的资料会以children属性的value呈现,所以在子元件中要用props.children来取得资料。
画面
在父元件中的Child标签中塞入html字串、、试试,子元件结构不动。
父元件
import React from 'react'
import './All.css';
function Father() {
return (
<div className="container">
<Child >
Text From Children
<hr/>
<div>this is element</div>
</Child>
</div>
)};
export default Father;
props.children会判断JSX语法的结构拆成三个不同的资料,以阵列的方式存放。
会将每个value依序渲染在画面上。
React有提供几个React.Children的函式让我们处理在子元件中接收props.children的资料。
React.Children.map(props.children,function[(thisArg)])}
React.Children.forEach(props.children, function[(thisArg)])
React.Children.count(props.children)
React.Children.only(props.children)
React.Children.toArray(props.children)
<<: Day12:今天来聊一下Parrot Security的OpenVAS
出於书本 Chapter 2. Cracking the Hacker Mindset 如果我是恶意...
报告班长,图片截自网路 大家有听过「报告班长」吗?这部 1987 年的电影,当年推出後一炮而红,带...
[Day 01] 单元测试是什麽?为什麽要做单元测试? 相信只要在资讯业待过一阵子, 一定都会听过单...
创建App-完结 30天的学习日记就这样慢慢结束啦! 从第一天的专案设计到现成的样子,过程学习、错误...
我手上的板子是L476RG,在当中一共有11个定时器: 其中分为基本、通用、高阶三种 基本定时器:T...