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的更多相关文章

  1. react 创建组件 (四)Stateless Functional Component

    上面我们提到的创建组件的方式,都是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Compo ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. React Creating Elements All In One

    React Creating Elements All In One https://reactjs.org/docs/react-api.html#creating-react-elements h ...

  7. [React] displayName for stateless component

    We can use 'displayName' on component to change its component tag in dev tool: import React from 're ...

  8. React Native(六)——PureComponent VS Component

    先看两段代码: export class ywg extends PureComponent { …… render() { return ( …… ); } } export class ywg e ...

  9. [React Native] Using the WebView component

    We can access web pages in our React Native application using the WebView component. We will connect ...

随机推荐

  1. java static 变量,和方法从属于类

    第36集 java static 变量,和方法从属于类 可以用类来直接调用static属性和方法 static方法不能调用非静态的属性和方法,反之可以 new产生的对象,不包括static 属性和方法

  2. ZOJ - 3195 Design the city

    题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...

  3. asp 下拉框二级联动

    <script language = "JavaScript"> //js开始 var aaa;//定义aaa变量 aaa=0;//aaa赋0 bb = new Arr ...

  4. protel dxp快捷键大全

    enter——选取或启动 esc——放弃或取消f1——启动在线帮助窗口tab——启动浮动图件的属性窗口pgup——放大窗口显示比例pgdn——缩小窗口显示比例end——刷新屏幕del——删除点取的元件 ...

  5. 145. Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  6. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  7. 协议(porotocol)

    fly.h: #import <Foundation/Foundation.h> @protocol Fly -(void) go; -(void) stop; @optional -(v ...

  8. STL 优先队列

    STL 栈,队列,优先队列用法 分类: Learning C++2013-11-15 00:52 843人阅读 评论(2) 收藏 举报 c++栈队列优先队列STL STL 中栈的使用方法(stack) ...

  9. Heartbeat+DRBD+NFS 构建高可用的文件系统

    1.实验拓扑图 2.修改主机名 1 2 3 vim /etc/sysconfig/network vim /etc/hosts drbd1.free.com     drbd2.free.com 3. ...

  10. DedeCMS 5.7 config.php 跨站脚本漏洞

    漏洞版本: DedeCMS 5.7 漏洞描述: DeDeCMS v5.7 在/include/dialog/config.php文件中存在XSS漏洞,攻击者可以利用该漏洞盗取用户Cookie.挂马等. ...