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. 洛谷 P2372 yyy2015c01挑战算周长

    P2372 yyy2015c01挑战算周长 题目背景 yyy2015c01快速的解决了问题,受到邻居们的赞许,高兴的回到家,把糖给了妈妈,吃了香喷喷的午饭,又睡了一个甜甜的午觉,感觉人生真美好.下午爸 ...

  2. 使用wget工具抓取网页和图片 及 相关工具几个

    想保存一些网页,最后找到这 wget 的 shell脚本,虽然不是太理想,亲测可用呢. 使用wget工具抓取网页和图片   来源 https://my.oschina.net/freestyletim ...

  3. c#的中英文混合字符串截取指定长度,startidx从0开始

    //c#的中英文混合字符串截取指定长度,startidx从0开始 by gisoracle@126.com public string getStrLenB(string str, int start ...

  4. 1、Task类构造函数

    Task类的构造函数接收一个无参无返回值的委托: 1: Task task = new Task(TaskMethod); 2: task.Start();例子:  task = new Task(( ...

  5. golang API 例子实现

    golang API 例子实现 http://files.cnblogs.com/files/rojas/astaxie.zip

  6. Codefroces 831B Keyboard Layouts

    B. Keyboard Layouts time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Redux简易理解

    1. createStore(相当于vuex的$store) 这才是数据存储仓库,用来存储初和输出的数据,更vuex$store功能一样 作用:  创建一个 Redux store 来以存放应用中所有 ...

  8. echarts tooltip提示框 自定义小圆点(颜色、形状和大小等等)

    项目是拿 echarts + 百度地图 来做可视化界面,现在到收尾阶段慢慢优化. 先附代码: formatter: function(params) { var result = '' params. ...

  9. vue中v-for的用法以及参数的作用

    <template> <div> <div class="content clearfix" v-on:click="goorderingD ...

  10. react实战项目-很适合进阶

    前言 前段时间学习完了React的基础,自己网上找了一些实战项目,做了几个感觉项目不是很全面,就想做一个完整的项目来提升自己的React水平.以前学习Vue的时候,就看过bailicangdu大神的v ...