博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
阅读量:4495 次
发布时间:2019-06-08

本文共 1257 字,大约阅读时间需要 4 分钟。

In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that React looks for when using map to create elements from a list. This HOC will allow us to wrap any component and it will take care of placing the keyprop on the component for us based on the property name that we specify.

 

Consider following code:

import React from 'react';import { render } from 'react-dom';const todos = [  { id: 1, name: 'Create the initial state', isComplete: true },  { id: 2, name: 'Map over data', isComplete: true },  { id: 3, name: 'Refactor', isComplete: true },  { id: 4, name: 'Use HOC to remove warning', isComplete: false },];const TodoItem = (todo) => (  
  • {todo.name}
  • );const App = (props) => (
      {props.todos.map(todo =>
      )}
    );render(
    , document.getElementById('root'));

    When we trying to render <TodoItem>, we use map function, just because we have to add 'key' attr to each TodoItem. We can improve this by using HoC. A HoC is just a function which takes component as arguement and return a function which render the component:

    const withKeyFromProps = (Comp, propName) => (props) => (  
    );

     

    ----

    转载于:https://www.cnblogs.com/Answer1215/p/6908577.html

    你可能感兴趣的文章
    10个CSS简写技巧让你永远受用
    查看>>
    Codeforces 476C Dreamoon and Sums (水
    查看>>
    Docker,Rkt谁会笑到最后
    查看>>
    文本界面听歌神器--moc
    查看>>
    Ubuntu上安装谷歌第二代机器学习系统TensorFlow
    查看>>
    JAVA设计模式之适配器模式
    查看>>
    CentOS安装Nginx 以及日志管理
    查看>>
    SEO总结(一)
    查看>>
    <HTML深入浅出> 读书笔记
    查看>>
    Java中将JSON对象转化为数组对象
    查看>>
    Linux:xargs命令详解
    查看>>
    :before伪元素的灵活用法——前置元素的装饰
    查看>>
    最后一周总结
    查看>>
    CT 来值班,让您安心过新年!
    查看>>
    杂七杂八快捷键
    查看>>
    [转]Hooked on DTrace
    查看>>
    BZOJ 1901 Dynamic Rankings
    查看>>
    Flex 布局教程:语法篇
    查看>>
    明天你好
    查看>>
    Spring 分散装配
    查看>>