Storing and updating values inside a component’s local state (known as controlled components) is such a common pattern that Preact offers a really handy feature called ‘link state’ that not only removes the need to bind class methods, but also handles the setting of new values. This can remove the need for ‘setter’ style methods on classes and in this lesson we’ll look at an example of tracking the value of a ‘text input’.

Install:

yarn add linkstate
import {h, Component} from 'preact';
import linkState from 'linkstate'; export default class TextInput extends Component {
render(props, {text = ''}) {
return (
<div>
<input type="text" value={text} onInput={linkState(this, 'text')}/>
<pre><code>{JSON.stringify(this.state, null, 2)}</code></pre>
</div>
)
}
}

linkState will help to call 'this.state.setState', (this, 'text'), first 'this' is the context, in which context, you want to call setState, second 'text' is the prop name of the state.

[PReact] Use Link State to Automatically Handle State Changes的更多相关文章

  1. 设计模式---状态变化模式之state状态模式(State)

    前提:状态变化模式 在组建构建过程中,某些对象的状态经常面临变化,如何对这些变化进行有效的管理?同时又维持高层模块的稳定?“状态变化”模式为这一个问题提供了一种解决方案. 典型模式 状态模式:Stat ...

  2. android wifi state and wifi ap state

    /** * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if * ...

  3. 三种线程不安全现象描述(escaped state以及hidden mutable state)

    hidden mutable state和escaped state是两种线程不安全问题:两者原因不同,前者主要是由于类成员变量中含有其他对象的引用,而这个引用是immutable的:后者是成员方法的 ...

  4. 发现未知字段 state ,过滤条件 [["state","not in",["draft"]]] 有误 的处理

    通常该问题出现的原因在于对象定义的state字段在view中并没有出现,解决方案就是在view中添加该字段即可.

  5. [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)

    While sometimes outside input can have influence on how a given stateful transaction transitions, th ...

  6. [Functional Programming] Define Discrete State Transitions using the State ADT

    We build our first state transactions as two discrete transactions, each working on a specific porti ...

  7. 状态模式 java && php

    状态模式 java && php     状态模式 输入信号是事件:输出是状态结果,状态模式状态机就是一个黑盒子.状态模式主要突出了两个字:”改变”,对象的状态决定了状态的行为,事物的 ...

  8. mina statemachine解读(二)

    这里主要讲下对外接口暴露的处理. // 创建对外接口对象 TaskWork taskWork = new StateMachineProxyBuilder().setStateContextLooku ...

  9. PHP设计模式之状态模式

    状态模式从字面上其实并不是很好理解.这里的状态是什么意思呢?保存状态?那不就是备忘录模式了.其实,这里的状态是类的状态,通过改变类的某个状态,让这个类感觉像是换了一个类一样.说起来有点拗口吧,先学习概 ...

随机推荐

  1. vue 遇到的报错

    [Vue warn]: Invalid default value for prop "dataParams": Props with type Object/Array must ...

  2. 【习题 8-4 UVA - 11491】Erasing and Winning

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑删掉第i位. 则第i+1位就会取代第i位. 则肯定第i+1位比第i位大的话,才比较好. 则从小到大贪心删,找到第一个a[i+1] ...

  3. 【Codeforces Round #428 (Div. 2) B】Game of the Rows

    [Link]:http://codeforces.com/contest/839/problem/B [Description] 给你n排的如题目所示的位置; 同一排中(1,2) 算相邻; (3,4) ...

  4. Ubuntu 美团sql优化工具SQLAdvisor的安装(转)

    by2009 by2009 发表于 3 个月前 SQLAdvisor简介 SQLAdvisor是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具.它基于MySQ ...

  5. HDU 5188 zhx and contest(带限制条件的 01背包)

    Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...

  6. Android学习笔记进阶18之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  7. php实现希尔排序(总结)

    php实现希尔排序(总结) 一.总结 1.希尔排序的算法思路:分组排序, 缩小增量排序,插入排序 2.算法思路: 循环非常好写 有几次gap:log2(n) 每次gap有几组:gap组 每组有几个元素 ...

  8. 1.21 Python基础知识 - python常用模块-2

    一.xml 什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 X ...

  9. VUE错误记录 - 小球模拟购物车

    <body> <div id="app"> <input type="button" value="Add to Car ...

  10. [USACO08JAN]电话线Telephone Lines(分层图)/洛谷P1948

    这道题其实是分层图,但和裸的分层图不太一样.因为它只要求路径总权值为路径上最大一条路径的权值,但仔细考虑,这同时也满足一个贪心的性质,那就是当你每次用路径总权值小的方案来更新,那么可以保证新的路径权值 ...