[React] React Fundamentals: Accessing Child Properties
When you're building your React components, you'll probably want to access child properties of the markup.
Parent
can read its children by accessing the special this.props.children
prop.this.props.children
is an opaque data structure: use the React.Children utilities to manipulate them.
https://facebook.github.io/react/docs/multiple-components.html
this.props.children undefined
You can't access the children of your component through this.props.children
.this.props.children
designates the children being passed onto you by the owner.
https://facebook.github.io/react/tips/children-undefined.html
Type of the Children props
Usually, a component's children (this.props.children
) is an array of components. However, when there is only a single child, this.props.children
will be the single child component itself without the array wrapper. This saves an array allocation.
https://facebook.github.io/react/tips/children-props-type.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>React Lesson 6: Accessing Child Properties</title>
<link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css"/>
</head>
<body> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script type="text/jsx">
/** @jsx React.DOM */ var App = React.createClass({
render: function(){
return (
<BButton>I <BHeart></BHeart> React</BButton>
);
}
}); var BButton = React.createClass({
render: function() {
return (
<a className="btn btn-primary">{this.props.children}</a>
);
}
}); var BHeart =
React.createClass({
render:function(){
return <span className="glyphicon glyphicon-heart"></span>
}
}); React.render(<App />, document.body);
</script>
</body>
</html>
App has two children BButton and BHeart, all thoes chilren come thought from {this.props.children}.
If you don't have {this.props.children}:
var BButton = React.createClass({
render: function() {
return (
<a className="btn btn-primary">No passed in!</a>
);
}
});
We end up with this:
[Notice:] Just remeber when give class to the render elements, we need to use 'className' not 'class'.
[React] React Fundamentals: Accessing Child Properties的更多相关文章
- [React Fundamentals] Accessing Child Properties
When you're building your React components, you'll probably want to access child properties of the m ...
- [React] React Fundamentals: transferPropsTo
the transferPropsTo method lets you easily push properties into your components to easily customize ...
- [React] React Fundamentals: Integrating Components with D3 and AngularJS
Since React is only interested in the V (view) of MVC, it plays well with other toolkits and framewo ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
- [React] react+redux+router+webpack+antd环境搭建一版
好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...
- React: React组件的生命周期
一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...
- React: React的属性验证机制
一.简介 在开发中,属性变量类型的验证,几乎是任何语言都必须关注的问题,因为如果传入的数据类型不对,轻者程序运行仅仅是给出警告⚠️,严重的会直接导致程序中断,APP闪退或者web页面挂掉,这是很严重的 ...
- React/react相关小结
React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...
随机推荐
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-007-定义切面的around advice
一.注解@AspectJ形式 1. package com.springinaction.springidol; import org.aspectj.lang.ProceedingJoinPoint ...
- VC 无标题栏对话框移动(在OnLButtonDown里再次发送消息)
操作系统:Windows 7软件环境:Visual C++ 2008 SP1本次目的:实现无框移动 所谓的无标题栏对话框,是基于对话框的工程,对话框属性Border设置为None,对话框如下所示: 为 ...
- QT做界面真是绝美,并且还可嵌入HTML与JS做界面(许多案例)
1. 这年头想要酷炫, 还是用web最方便, QT自带嵌入式webkit, 然后用d3.js D3.js - Data-Driven Documents什么的, 各种酷炫的互动表随手而来.这里有各种各 ...
- yiic 数据库迁移工具
数据库的结构也同源代码一样随着我们开发的进行而不断的发生着改变.在开发过程中,一般的我们需要像管理我们的源代码一样记录下数据库结构的整个变化过程,以便代码还原到指定版本后,数据库能同步的还原到指定的版 ...
- USACO3.24Feed Ratios
直接枚举到100水过 /* ID: shangca2 LANG: C++ TASK: ratios */ #include <iostream> #include<cstdio> ...
- Java Memory Management(1)
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...
- POJ 3020 Antenna Placement 解题报告
题意就不说了,求二部图最大匹配. 问题是怎么建图…… 给定的条件中,h<40,w<10,所以笔者直接默认所有情况的地图都是40*10,当然,超出范围的便是空城o. 然后给城市编号.一个城市 ...
- 使用 ASR 和 Azure Pack 为 IaaS 工作负荷提供托受管 DR
Ashish Gangwar 云 + Enterprise项目经理 几周前,我们宣布了在 Azure Site Recovery 中提供一些新功能,这些新功能适用于不同场景,可以让服务提供商在 A ...
- apache开源项目--Ignite
Apache Ignite 内存数组组织框架是一个高性能.集成和分布式的内存计算和事务平台,用于大规模的数据集处理.Ignite 为应用和不同的数据源之间提供一个高性能.分布式内存中数据组织管理的框架 ...
- HDU-5414 CRB and String
http://acm.hdu.edu.cn/showproblem.php?pid=5414 题意:给定字符串s和t,可以在s里面选一个字符c,然后任选一个字符d(d!=c)将d插入到c的后面,问能不 ...