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. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

    一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...

  2. USB Type-C 连接器规范推出之后,市场很多低质量线材容易损坏设备

    USB Type-C 连接器规范推出之后,已有不少行动装置产品使用,其中最知名的产品为 Apple MacBook,机身仅提供一组 Type-C 端口,同时兼具充电与数据传输之用.市面上第三方厂商也开 ...

  3. open MMT.distributions = null on transaction type: WIP Lot Split

    open MMT.distributions = null on transaction type:  WIP Lot Split       打开物料事务处理界面,发现事务处理类型为:WIP Lot ...

  4. USACO4.12Beef McNuggets(背包+数论)

    昨天晚上写的一题 结果USACO一直挂中 今天交了下 有一点点的数论知识  背包很好想 就是不好确定上界 官方题解: 这是一个背包问题.一般使用动态规划求解. 一种具体的实现是:用一个线性表储存所有的 ...

  5. Hibernate java.lang.NoSuchFieldError: INSTANCE

    在使用hibernate3.6.2是我遇到了一个有趣的错误java.lang.NoSuchFieldError: INSTANCEat org.hibernate.type.BasicTypeRegi ...

  6. Linux Kernel 本地内存损坏漏洞

    漏洞名称: Linux Kernel 本地内存损坏漏洞 CNNVD编号: CNNVD-201310-663 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等级:    漏洞类 ...

  7. SharePoint2010主题和样式揭秘

    转:http://www.cnblogs.com/Ryu666/archive/2011/07/28/2119652.html 好久好久没写技术博客了,差点以为技术已经离我远去.但鱼离不开水,我怎能把 ...

  8. EventHandlerList z

    写一个类时,有时候会在同一个类上添加很多事件,事件很多的话,是不容易管理的,.NET提供的EventHandlerList可以辅助多个事件的管 理,但不方便的地方是,它不是类型安全的,缺少类型安全,多 ...

  9. NHibernate统一类封装代码

    NHibernate已经成为.net主流的ORM框架,当然,在开发中如果需要使用NHibernate的话,我们一般会对她进行一次封装,以便在项目中使用更方便,以及对NHibernate有一个全局的控制 ...

  10. [codevs3295]落单的数

    题目描述 Description 有n个数(n是奇数),其中n-1个数两两成对,有1个数落单,找出这个数.要求O(n)的时间复杂度,O(1)的空间复杂度 输入描述 Input Description ...