[React] Creating a stateless functional Component

Source: Internet
Author: User

The most of the components of this you write would be stateless, meaning that they take in props and return what do you want to be di Splayed. In React 0.14, a simpler syntax for writing these kinds of the components were introduced, and we began calling these component S "Stateless functional Components". In this lesson, let's take a look at what define a stateless function component, and how to integrate useful React Featu Res like Prop Type validation and using this new component syntax.

Compnents with state:

class Title extends React.component {  render () {    return  (       .props.value}    )  }}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) = (  ) class App extends react.component {  render () {    return  (      <title value= "Hello world!"/>     )  }} Reactdom.render (  <app/>,  document.queryselector ("#root"))

So are 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's still possible:

 /*  class Title extends React.component { Render () {return ( */ Span style= "color: #000000;" >const Title  = (props) => ( ) title.proptypes  = {value:React.PropTypes.string.isRequired} Title.defaultprops  = {value:  "Egghead.io is awson!!" }class App extends React.component {render () {   ( <title value= "Hello world!"/>"}}reactdom.render ( <app/>, Document.queryselector ("#root"  

Statless compoennt rendering much fast than extends one.

[React] Creating a stateless functional Component

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.