Preact offers, in addition to the regular component API from React, the ability to access both props & state as function parameters to the render method. This lesson will cover an example of how to utilize this convenience along with how destructuring can make it even nicer to work with.

import {h, Component} from 'preact';
import User from './User'; export default class App extends Component {
constructor(props) {
super(props); this.state = {
loading: true,
user: null
};
} componentDidMount() {
fetch(this.props.config.urls.user)
.then(resp => resp.json())
.then(user => {
this.setState({
user,
loading: false
});
})
.catch(err => console.error(err));
} // render(props, state) {
render({config}, {loading, user}) {
return (
<div class="app">
{loading
? <p>Fetching {config.urls.user}</p>
: <User image={user.avatar_url}
name={user.name} />
}
</div>
);
}
}

[Preact] Use State and Props in the Component Render Function的更多相关文章

  1. [Vue warn]: You may have an infinite update loop in a component render function

    [Vue warn]: You may have an infinite update loop in a component render function 这个问题很奇怪,之前从来没有遇到过.如果 ...

  2. React应用程序设计过程中如何区分模块到底是state还是props?

    根据官方文档,满足以下任意条件的模块,就不是State,原文如下: 1.Is it passed in from a parent via props? If so, it probably isn’ ...

  3. 组件的三大属性state,props,refs与事件处理

    组件的三大属性state state是组件对象最重要的属性, 值是对象(可以包含多个数据),组件被称为"状态机", 通过更新组件的state来更新对应的页面显示(重新渲染组件) 初 ...

  4. react 中state与props

    react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...

  5. React.js 小书 Lesson12 - state vs props

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson12 转载请注明出处,保留原文链接和作者信息. 我们来一个关于 state 和 props 的 ...

  6. state vs props

    我们来一个关于 state 和 props 的总结. state 的主要作用是用于组件保存.控制.修改自己的可变状态.state 在组件内部初始化,可以被组件自身修改,而外部不能访问也不能修改.你可以 ...

  7. react基础语法(五) state和props区别和使用

    props的验证: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  8. react --- React中state和props分别是什么?

    props React的核心思想就是组件化思想,页面会被切分成一些独立的.可复用的组件. 组件从概念上看就是一个函数,可以接受一个参数作为输入值,这个参数就是props,所以可以把props理解为从外 ...

  9. React评论展示案例(包含知识点:state、props、ref、React声明周期、localStorage本地存储等)

    本案例在上一篇的案例(React组件之间通过Props传值的技巧(小案例,帮助体会理解props.state.受控组件和非受控组件等))的基础上加强功能和用户体验,但是当然还有很多需要改进的地方,后期 ...

随机推荐

  1. 使用PHP中的curl发送请求

    使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 初始化连接句柄: 设置CURL选项: 执行并获取结果: 释放VURL连接句柄. 下面的程序片段是 ...

  2. 【t038】&&【u214】金字塔

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小X来到一个雄奇的金字塔挖宝,但是这是一座被诅咒的金字塔,小X必须马上逃离这里,否则小X就会被埋在金字 ...

  3. 【2017 Multi-University Training Contest - Team 4】Time To Get Up

    [Link]: [Description] [Solution] 把每个数字长什么样存到数组里就好;傻逼题. (直接输入每一行是什么样子更快,不要一个字符一个字符地输入) [NumberOf WA] ...

  4. CodeForcesGym 100502H Clock Pictures

    Clock Pictures Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGy ...

  5. [React Native] Animate the Scale of a React Native Button using Animated.spring

    In this lesson we will use Animated.spring and TouchableWithoutFeedback to animate the scale of a bu ...

  6. FormatMessage函数的使用方法

    使用FormatMessage时假设对一些參数不细致研究.那么就会出错误.首先说下这个函数 1 函数描写叙述 DWORD WINAPI FormatMessage( _In_ DWORD dwFlag ...

  7. badblocks 检查硬盘是否有坏道

    硬盘是比較easy坏掉的设备,使用一段时间后可能会出现坏道等物理故障. 当硬盘出现坏道后,若不及时更换或者进行技术上的处理,磁盘的坏道就会越来越多,并会造成频繁死机和数据丢失. 最好的处理方法是更换新 ...

  8. Android自定义视图

    Android框架为我们提供了大量的视图类来帮助我们做好展示信息以及同用户进行交互的工作.然后有时候,我们的app或许需要一些在Android内建视图之外特殊的视图,那么此时我们就需要自定义视图.下面 ...

  9. gridView -item 大小调节(dimen-代码引用)

    今天在修改一个gridview的时候,发现里面的内容并不会自动适应,填满整个gridview,而是会产生滑动,尝试了很多的方法,包括在item文件中设定width和height,结果,宽度可调,高度却 ...

  10. Android RecyclerView嵌套RecyclerView

    原理 RecyclerView嵌套RecyclerView的条目,项目中可能会经常有这样的需求,但是我们将子条目设置为RecyclerView之后,却显示不出来.自己试了很久,终于找到了原因:必须先设 ...