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. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  2. 【DataStructure In Python】Python模拟链表

    最近一直在学习Python和Perl这两门语言,两者共同点很多,也有不多.希望通过这样的模拟练习可以让自己更熟悉语言,虽然很多时候觉得这样用Python或者Perl并没有体现这两者的真正价值. #! ...

  3. WIP_DISCRETE_JOBS.STATUS_TYPE

    WIP_DISCRETE_JOBS.STATUS_TYPE Value Meaning 7 Cancelled 8 Pending Bill Load 9 Failed Bill Load 10 Pe ...

  4. MySQL 线上配置文件

    [client] port            = 3306 socket          = /tmp/mysql.sock default-character-set = utf8 [mysq ...

  5. C# DataGridView添加新行的2个方法

    可以静态绑定数据源,这样就自动为DataGridView控件添加 相应的行.假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行 ...

  6. CodeForces 400

    A - Inna and Choose Options Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  7. poj 1184 广搜进阶题

    起初的想法果然就是一个6000000的状态的表示. 但是后面觉得还是太过于幼稚了. 可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小. #include<iostream> # ...

  8. 在ASP.NET MVC中实现基于URL的权限控制

    本示例演示了在ASP.NET MVC中进行基于URL的权限控制,由于是基于URL进行控制的,所以只能精确到页.这种权限控制的优点是可以在已有的项目上改动极少的代码来增加权限控制功能,和项目本身的耦合度 ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.2

    Show that the following statements are equivalent: (1). $A$ is positive. (2). $A=B^*B$ for some $B$. ...

  10. 处理Google Play的相关方法

    1.打开Google play软件的详细页面 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.se ...