[React] Refactor a Stateful List Component to a Functional Component with React PowerPlug

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React] Refactor a Stateful List Component to a Functional Component with React PowerPlug相关的知识,希望对你有一定的参考价值。

In this lesson we‘ll look at React PowerPlug‘s <List /> component by refactoring a normal class component with state and handlers to a functional component powered by React PowerPlug.

 

import React from "react";
import { render } from "react-dom";
import random from "random-name";
import { List } from "react-powerplug";

function MyList() {
  return (
    <List initial={["Jago", "Cinder", "Glacius", "Riptor"]}>
      {({ list, push, pull }) => (
        <div>
          <div className="block">
            {list.map(name => (
              <span
                key={name}
                className="tag is-link"
                style={{ marginRight: 10 }}
              >
                <button
                  className="delete is-small"
                  style={{ marginRight: 5 }}
                  onClick={() => pull(n => n === name)}
                />
                {name}
              </span>
            ))}
          </div>
          <button
            className="button is-success"
            onClick={() => push(random.first())}
          >
            Add Random Name
          </button>
        </div>
      )}
    </List>
  );
}
render(<MyList />, document.getElementById("root"));

 

以上是关于[React] Refactor a Stateful List Component to a Functional Component with React PowerPlug的主要内容,如果未能解决你的问题,请参考以下文章

[React] Refactor a connected Redux component to use Unstated

[React] Refactor componentWillReceiveProps() to getDerivedStateFromProps() in React 16.3

[Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose

[Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

[JS Compose] 1. Refactor imperative code to a single composed expression using Box

IDEA快捷键拆解系列(八):Refactor篇