博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] Creating a Stateless Functional Component
阅读量:4319 次
发布时间:2019-06-06

本文共 1642 字,大约阅读时间需要 5 分钟。

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 (      

{
this.props.value}

) }}class App extends React.Component { render(){ return ( ) }}ReactDOM.render(
, document.querySelector("#root"))

 

Conver Title component to stateless component:

const Title =  (props) => (  

{props.value}

)class App extends React.Component { render(){ return ( ) }}ReactDOM.render(
, 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 (      

{this.props.value}

) }}*/const Title = (props) => (

{props.value}

)Title.propTypes = { value: React.PropTypes.string.isRequired}Title.defaultProps = { value: "Egghead.io is Awson!!"}class App extends React.Component { render(){ return ( ) }}ReactDOM.render(
, document.querySelector("#root"))

Statless compoennt rendering much fast than extends one.

转载于:https://www.cnblogs.com/Answer1215/p/5642209.html

你可能感兴趣的文章
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
UVALive 6145 Version Controlled IDE(可持久化treap、rope)
查看>>
mysql 将两个有主键的表合并到一起
查看>>
底部导航栏-----FragmentTabHost
查看>>
在linux中安装jdk以及tomcat并shell脚本关闭启动的进程
查看>>
apk,task,android:process与android:sharedUserId的区别
查看>>
网络字节顺序
查看>>
复制mueclipse项目到eclipse
查看>>
飞扬的小鸟
查看>>
玩转TypeScript(2) --简单TypeScript类型
查看>>
Asp.net 解析json
查看>>
程序猿崛起3——这一次,我用行动说话
查看>>
201521123038 《Java程序设计》 第一周学习总结
查看>>
每天一个linux命令(20):find命令之exec
查看>>
MVC HtmlHelper用法大全
查看>>