[React] Creating a Stateless Functional Component
Most of the components that you write will be stateless, meaning that they take in props and return what you want to be displayed. In React 0.14, a simpler syntax for writing these kinds of components was introduced, and we began calling these components "stateless functional components". In this lesson, let's take a look at how to define a stateless function component, and how to integrate useful React features like Prop Type validation while using this new component syntax.
Compnents with State:
class Title extends React.Component {
render(){
return (
<h1>{this.props.value}</h1>
)
}
} class App extends React.Component {
render(){
return (
<Title value="Hello World!" />
)
}
} ReactDOM.render(
<App />,
document.querySelector("#root")
)
Conver Title component to stateless component:
const Title = (props) => (
<h1>{props.value}</h1>
) class App extends React.Component {
render(){
return (
<Title value="Hello World!" />
)
}
} ReactDOM.render(
<App />,
document.querySelector("#root")
)
So now you cannot access lifecycle hooks, anyway a dump compoennt doesn't need to handle those lifecycle hooks.
But if you want to set defaultProps and propTypes, it is still possible:
/*class Title extends React.Component {
render(){
return (
<h1>{this.props.value}</h1>
)
}
}
*/
const Title = (props) => (
<h1>{props.value}</h1>
)
Title.propTypes = {
value: React.PropTypes.string.isRequired
}
Title.defaultProps = {
value: "Egghead.io is Awson!!"
} class App extends React.Component {
render(){
return (
<Title value="Hello World!" />
)
}
} ReactDOM.render(
<App />,
document.querySelector("#root")
)
Statless compoennt rendering much fast than extends one.
[React] Creating a Stateless Functional Component的更多相关文章
- react 创建组件 (四)Stateless Functional Component
上面我们提到的创建组件的方式,都是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Compo ...
- [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...
- [React] Return a list of elements from a functional component in React
We sometimes just want to return a couple of elements next to one another from a React functional co ...
- [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug
In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal clas ...
- [React] Use React.memo with a Function Component to get PureComponent Behavior
A new Higher Order Component (HOC) was recently released in React v16.6.0 called React.memo. This be ...
- React Creating Elements All In One
React Creating Elements All In One https://reactjs.org/docs/react-api.html#creating-react-elements h ...
- [React] displayName for stateless component
We can use 'displayName' on component to change its component tag in dev tool: import React from 're ...
- React Native(六)——PureComponent VS Component
先看两段代码: export class ywg extends PureComponent { …… render() { return ( …… ); } } export class ywg e ...
- [React Native] Using the WebView component
We can access web pages in our React Native application using the WebView component. We will connect ...
随机推荐
- mac 浏览器 强刷快捷键
windows 浏览器的刷新快捷键F5,强制刷新Ctrl+F5 Mac 系统下浏览器的刷新快捷键 command+R, 强制刷新快捷键为 command+shift+R
- GridView 根据要求显示指定值
最近在写一个小项目用来练手恢复一下功力的,在Users表中有一个用户字段是状态,我使用"0"表示启用,“1”表示禁用, 存到数据库中, 由于之前有一段时间没写代码了,所以有点生疏了 ...
- Using FireMonkey Layouts
FireMonkey has many layout controls to choose from. Come learn the differences and how to use them t ...
- ArcGis 在线地图相关资源
原文:ArcGis 在线地图相关资源 世界边界和地点:http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Bou ...
- 【HDOJ】1542 Atlantis
离散化+线段树+扫描线,求覆盖面积. /* 1542 */ #include <iostream> #include <string> #include <map> ...
- VM上成功安装mac os x
想必很多人在intel . AMD上安装苹果系统,总会遇到很多问题,今天我就将我成功安装的过程给大家分享一下. 下面是我在VM上安装的步骤: 1. 准备软件 OS X Snow Leopard 10. ...
- HTML5实现在线抓拍
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- ruby编程语言-学习笔记3(第4章 表达式和操作符)
4.6 操作符 了解优先级很重要 位移操作符 (0b1011)<< 1 # ==> "10110" 11 << 1 = 22 ...
- 分享form表单提交问题
前段时间做了一个form表单传值问题 当时觉得form表单的submit不就是提交form表单name的value值吗 ? 其实是对的 但是我做的是一个打印页面 需要把当前页面的元素传入下一个u ...
- OpenGL三维镂垫
2015-12-12帮舍友尝试这个代码的时候发现舍友的会出现No GLSL support 后来发现舍友的版本2.0.2.1才能用 舍友的是glutInitContextVersion(3, 1);改 ...