Z
Z
zjoin2015-07-05 15:23:40
JavaScript
zjoin, 2015-07-05 15:23:40

How to make such markup in React.js?

050593da002649b28016adbbdec5fa77.png
There is such a code, everything seems to be correct, but items does not display. or is there an easier way?

let footerColumns = [
   {
     name: "О компании",
     items: {
       route1: "sdf",
       route2: "sdfsdf"
     }
   },
   {
     name: "Потребителям",
     items: {
       route1: "sdf",
       route2: "sdfsdf",
       route3: "qwert"
     }
   }
]

class Footer extends React.Component {
    renderColumnItems(items) {
      console.log(items);
      return (
        <ul>
          {Object.keys(items).map((route) => {
            <li>
              <a href="#">
                {items[route]}
              </a>
            </li>
          })}
        </ul>
      );
}

    render() {
      return (
        <div>
          {footerColumns.map((column) => {
            return (
              <div className="column footer">
                <h3>{column.name}</h3>
                {this.renderColumnItems(column.items)}
              </div>
            );
          })}
        </div>
      );
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lesha Fedoseev, 2015-07-05
@zjoin

const footerColumns = [
   {
     name: "О компании",
     items: {
       route1: "sdf",
       route2: "sdfsdf"
     }
   },
   {
     name: "Потребителям",
     items: {
       route1: "sdf",
       route2: "sdfsdf",
       route3: "qwert"
     }
   }
];

class Footer extends React.Component {
  
  constructor(props) {
    super(props);
  }
  
  
  _renderColumnItems(items) {
    
    // Если есть возможность изначально сделать `items` массивом,
    // то лучше сделать его массивом
    
    let itemsArray = [];
      
    for (let item in items) {
      if (items.hasOwnProperty(item)) {
        itemsArray.push({
          route: item,
          name : items[item]
        });
      }
    }
        
    return (
      <ul>
        {itemsArray.map(item => (
          <li>
            <a href="#">{item.name}</a>
          </li>
        ))}
      </ul>
    );
  }


  render() {
    
    const { columns } = this.props;
    
    return (
      <div>
        {columns.map(column => (
          <div className="column footer">
            <h3>{column.name}</h3>
            {this._renderColumnItems(column.items)}
          </div>
        ))}
      </div>
    );
  }
}

React.render(<Footer columns={footerColumns} />, document.getElementById('app'));

Z
zjoin, 2015-07-05
@zjoin

map of undefined

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question