码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-31 11:19:04      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:import   nal   func   list   blog   ring   code   map   turn   

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

标签:import   nal   func   list   blog   ring   code   map   turn   

原文地址:https://www.cnblogs.com/Answer1215/p/8390358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!