[PReact] Use Link State to Automatically Handle State Changes

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.

时间: 2024-08-05 12:13:03

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

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

前提:状态变化模式 在组建构建过程中,某些对象的状态经常面临变化,如何对这些变化进行有效的管理?同时又维持高层模块的稳定?“状态变化”模式为这一个问题提供了一种解决方案. 典型模式 状态模式:State 备忘录模式:Memento 一:State状态模式 (一)概念 允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类 状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况.把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化. (二)动机 在软

android wifi state and wifi ap state

/** * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if * it finishes successfully. * * @see #WIFI_STATE_CHANGED_ACTION * @see #getWifiState() */ public static final int WIFI_STATE_DISABLING = 0; /** * Wi-Fi

[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, there are many times where the current state at the time of a transaction. We can see the power of this type of transaction by seeing what it would take

状态模式 java &amp;&amp; php

状态模式 java && php 状态模式 输入信号是事件:输出是状态结果,状态模式状态机就是一个黑盒子.状态模式主要突出了两个字:”改变”,对象的状态决定了状态的行为,事物的本质决定了事物的行为.对一个状态特别重要的对象,通过状态机来建模一个对象的状态:状态模式处理的核心问题是状态的迁移,因为在对象存在很多状态情况下,对各个business flow,各个状态之间跳转和迁移过程都是及其复杂的.例如一个工作流,审批一个文件,存在新建.提交.已修改.HR部门审批中.老板审批中.HR审批失败.

状态模式的另一种写法

<?php /** * 状态模式 * 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它所属的类 */ /*接口*/ interface State { public function handle($state);//切换到下一个状态 public function display();//显示当前状态 } /*状态A*/ class StateA implements State { public function handle($context) { $context->

设计模式学习(十七) 状态模式

场景: -- 酒店系统中,房间的状态变化:   空闲 -- >已预定 -- > 已入住 核心: -- 用于解决系统中复杂对象的状态转换以及不同状态下行为的封装问题 结构: -- Context环境类 环境类中维护一个State对象,他是定义了当前的状态 -- State抽象状态类 -- 每一个类封装了一个状态对应的行为 代码实现: package state; public class HomeContext { private State state; public void setStat

趣谈状态模式

全文一共1543字,预计阅读时间10分钟 定义: 状态模式(State),当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 只看这个定义的话,想必会一头雾水,其实状态模式解决的问题是: 当控制了一个对象状态转换的表达式过于复杂时,我们可以把状态的判断逻辑转移到表示不同状态的一系列的类中.这样做可以使复杂的判断逻辑简化,同时使类的职责更加单一. 实例: 假设每一个程序员会对应一个经验值(empiricalValue),我们会根据这个程序员的经验值,来评定这个程序员的职称,如

Flink - state

  public class StreamTaskState implements Serializable, Closeable { private static final long serialVersionUID = 1L; private StateHandle<?> operatorState; private StateHandle<Serializable> functionState; private HashMap<String, KvStateSnaps

PatentTips - Zero voltage processor sleep state

BACKGROUND Embodiments of the invention relate to the field of electronic systems and power management. More particularly, embodiments of the invention relate to a method and apparatus for a zero voltage processor sleep state. As the trend toward adv