Rendering comma separated list of links

后端 未结 10 1574
再見小時候
再見小時候 2020-12-24 00:58

I\'m trying to output a list of comma separated links and this is my solution.

var Item = React.createComponent({
  render: function() {

    var tags = [],
         


        
相关标签:
10条回答
  • 2020-12-24 01:17

    To add to the great answers above Ramda has intersperse.

    To comma separate a bunch of items you could do:

    const makeLinks = (x: Result[]) =>
      intersperse(<>,</>, map(makeLink, x))
    

    Pretty succinct

    0 讨论(0)
  • 2020-12-24 01:21

    Or simply write the list items to an unordered list and use CSS.

    var Item = React.createComponent({
      render: function() {
    
        var tags = this.props.item.tags.map(function(i, item) {
          return <li><Tag key={i} tag={item} /></li>
        });
    
        return (
          <tr>
            <td>
              {this.props.item.name}
            </td>
            <td>
              <ul className="list--tags">
                {tags}
              </ul>
            </td>
          </tr>
        );
    
      }
    });
    

    And the CSS:

    .list--tags {
        padding-left: 0;
        text-transform: capitalize;
    }
    
    .list--tags > li {
        display: inline;
    }
    
    .list--tags > li:before {
        content:',\0000a0'; /* Non-breaking space */
    }
    .list--tags > li:first-child:before {
        content: normal;
    }
    
    0 讨论(0)
  • 2020-12-24 01:22
    import React from 'react';
    import { compact } from 'lodash';
    
    // Whatever you want to separate your items with commas, space, border...
    const Separator = () =>  { ... }; 
    
    // Helpful component to wrap items that should be separated
    const WithSeparators = ({ children, ...props }) => {
    
      // _.compact will remove falsey values: useful when doing conditional rendering
      const array = compact(React.Children.toArray(children));
    
      return array.map((childrenItem, i) => (
        <React.Fragment key={`${i}`}>
          {i > 0 && <Separator {...props} />}
          {childrenItem}
        </React.Fragment>
      ));
    };
    
    const MyPage = () => (
      <WithSeparators/>
        <div>First</div>
        {second && (<div>Maybe second</div>)}
        {third && (<div>Maybe third</div>)}
        <div>Fourth</div>
      </WithSeparators>
    );
    
    0 讨论(0)
  • 2020-12-24 01:25

    At Khan Academy we use a helper called intersperse for this:

    /* intersperse: Return an array with the separator interspersed between
     * each element of the input array.
     *
     * > _([1,2,3]).intersperse(0)
     * [1,0,2,0,3]
     */
    function intersperse(arr, sep) {
        if (arr.length === 0) {
            return [];
        }
    
        return arr.slice(1).reduce(function(xs, x, i) {
            return xs.concat([sep, x]);
        }, [arr[0]]);
    }
    

    which allows you to write code like:

    var tags = item.tags.map(function(tag, i) {
        return <Tag key={i} tag={item.tags[i]} />;
    };
    tags = intersperse(tags, ", ");
    
    0 讨论(0)
  • 2020-12-24 01:29

    Easiest way to do

        const elementsArr = ["a","b,"c"];
        let elementsToRender = [] ;
        elementsArr.forEach((element, index) => {
            let elementComponent = <TAG className="abc" key={element.id}>{element}</TAG>
            elementsToRender.push(elementComponent);
            if(index !== (elementsArr.length - 1)){
                elementsToRender.push(", ");
            }
        });
    
        render(){
            return (
                <div>{elementsToRender}</div>
            )
        }
    
    0 讨论(0)
  • 2020-12-24 01:32

    Simply

    {tags.map((tag, i) => <span key={i}>
        {i > 0 && ", "}
        <Tag tag={tag} />
    </span>)}
    


    In React 16 it can be done even more simpler:

    {tags.map((tag, i) => [
        i > 0 && ", ",
        <Tag key={i} tag={tag} />
    ])}
    
    0 讨论(0)
提交回复
热议问题