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

时间: 2024-12-22 05:44:19

[React] Creating a Stateless Functional Component的相关文章

[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 looks for when using map to create elements from a list. This HOC will allow us to wrap any component and it will take care of placing the keypr

[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 class component with state and handlers to a functional component powered by React PowerPlug. import React from "react"; import { render } from "

[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 component, without adding a wrapper component which only purpose would be to wrap elements that we want to render. In this one minute lesson we are goin

[React Native] Using the WebView component

We can access web pages in our React Native application using the WebView component. We will connect the links in our repository component to their Github web page when a user click on them. Navigate to WebView component: openPage(url){ this.props.na

[React] Preview and edit a component live with React Live

In this lesson we'll use React Live to preview and edit a component directly in the browser. React Live is a great tool for rendering interactive documentation or any place you would like to demonstrate a React component with a live preview and editi

002-and design-dva.js 知识导图-01JavaScript 语言,React Component

一.概述 参看:https://github.com/dvajs/dva-knowledgemap react 或 dva 时会不会有这样的疑惑: es6 特性那么多,我需要全部学会吗? react component 有 3 种写法,我需要全部学会吗? reducer 的增删改应该怎么写? 怎么做全局/局部的错误处理? 怎么发异步请求? 怎么处理复杂的异步业务逻辑? 怎么配置路由? 二.JavaScript 语言 2.1.变量声明 const 和 let 不要用 var,而是用 const 和

使用Formik轻松开发更高质量的React表单(四)其他几个API解析

(下面部分内容正处于翻译中,敬请稍等......) <Field /> <Field /> will automagically hook up inputs to Formik. It uses the name attribute to match up with Formik state. <Field /> will default to an <input /> element. To change the underlying element o

翻译: TypeScript 1.8 Beta 发布

原文地址:https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/ 今天,我们发布了 TypeScript 1.8 Beta,伴随着 1.8 版本带来了大量的变化.欢迎使用并反馈给我们:send us your feedback. 可以从  Visual Studio 2015, NuGet (Compiler 和 MSBuild task), npm, 或者直接从 source 

[Vue @Component] Pass Props to Vue Functional Templates

Functional templates allow you to create components consisting of only the template tag and exposing the props passed into the template with the props object off of the template context. This approach allows you to build simple configurable templates