[Recompose] Add Local State to a Functional Stateless Component using Recompose

Learn how to use the ‘withState‘ and ‘withHandlers‘ higher order components to easily add local state to—and create a reusable local state pattern for—your functional stateless components. No need for classes!

withState:

const Statue = withState(‘show‘, ‘toggleShow‘, false)(
    ({ status, show, toggleShow }) =>
        (<span onClick={() => toggleShow((val) => !val)}>
            {status}
            {show && <StatusList/>}
        </span>)
);

withState, we create a variable ‘show‘ and a function ‘toggleShow‘, the default value for ‘show‘ is false. Now the variable and function are injected into our stateless component as props.

The function ‘toggleShow‘ can just take a value as arguement or it can also take function as an arguement.

take as function:

onClick={() => toggleShow((val) => !val)}

take as value:

onClick={() => toggleShow(!show)}

withHandlers & compose:

To make withState more reuseable, we can use ‘withHandler‘ & ‘compose‘ to create an HoC.

const withToggle = compose(
    withState(‘toggleState‘, ‘toggleShow‘, false),
    withHandlers({
        toggle: ({toggleShow}) => (e) => toggleShow((val) => !val),
        show: ({toggleShow}) => (e) => toggleShow(true),
        hide: ({toggleShow}) => (e) => toggleShow(false)
                 })
);

const Statue = withToggle(
    ({ status, toggle, toggleState }) =>
        (<span onClick={() => toggle(!toggleState)}>
            {status}
            {toggleState && <StatusList/>}
        </span>)
);

const Tooltip = withToggle(({ show, hide, toggleState, text, children }) => (
    <span>
        <span>
            {toggleState && <div
                style={TooltipStyle}>
                { text }
            </div>}
             <span
                 onMouseOver={show}
                 onMouseOut={hide}
             >
                { children }
            </span>
        </span>
    </span>
));
时间: 2025-01-02 15:41:13

[Recompose] Add Local State to a Functional Stateless Component using Recompose的相关文章

[Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose

Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a class component. import React from 'react'; import { withReducer, withHandlers, compose, lifecycle } from 'recompose'; // Mock Configuration function fe

[Recompose] Add Local State with Redux-like Reducers using Recompose

Learn how to use the 'withReducer' higher order component using the alternative reducer form. If you like using reducers in redux, you’ll be able to reuse that same technique but for local state. import React from 'react'; import {withReducer, withHa

react常见组件问题Can&#39;t perform a React state update on an unmounted component

在些react组件的时候,会有这个警告 Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method” 这是因为在写一

react项目Bug:组件销毁清除监听(Can&#39;t perform a React state update on an unmounted component.)

最近一直在做react项目,发现一个bug,困扰了我两天. Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount metho

[Preact] Use State and Props in the Component Render Function

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 destructurin

5月面试题总结

HTML Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于HTML文档中的第一行,处于 <html> 标签之前.告知浏览器的解析器用什么文档标准解析这个文档.DOCTYPE不存在或格式不正确会导致文档以兼容模式呈现. (2).标准模式的排版 和JS运作模式都是以该浏览器支持的最高标准运行.在兼容模式中,页面以宽松的向后兼容的方式显示,模拟老式浏览器的行为以防止站点无法工作. HTML5 为什么只需要写 <!DOCTYPE HTML&g

[React] Creating a Stateless Functional Component

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 compon

[MST] Create an Entry Form to Add Models to the State Tree

It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we've built so far. In this lesson you will learn: MST is not limited to a single state tree. Every model can form a tree on its own Appending model insta

[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 handle