Rangle.io: React Training
master
master
  • Rangle React Training Materials
  • lessons
    • redux-hello
    • react-summary
    • redux-action-reducer
    • react-outline
    • react-container
    • redux-intro
    • redux-connect
    • react-component
    • advanced
    • redux-store
    • redux-logging
    • redux-thunk
    • react-cardlist
    • redux-merge
    • react-lifecycles
    • react-intro
  • CONDUCT
  • misc
    • summary
    • refs
    • overview
    • guide
    • gloss
  • LICENSE
  • index
  • rough-slides
    • react-training-slides
  • CONTRIBUTING
Powered by GitBook
On this page
  • Goals
  • Connecting the Pieces
  • The Card List
  • PropTypes

Was this helpful?

  1. lessons

react-cardlist

Goals

  • Copying and pasting cards is tedious, so we create a CardList component

  • Hard-code the data as an array in robots.js to start with

export const robots = [
  {
    id: 1,
    name: "Leanne Graham",
    username: "Bret",
    email: "Sincere@april.biz"
  },
  ...
}

Connecting the Pieces

  • Then pass that array to CardList in index.js

ReactDOM.render(
  <div>
    <CardList robots={robots} />
  </div>,
  document.getElementById("root")
);

The Card List

  • CardList transforms the array of values into an array of views using map

    • Wraps that array in a div because it can only return one thing

    • JSX automatically concatenates array elements for us

const CardList = ({ robots }) => {
  const cardsArray = robots.map(robot => (
    <Card
      name={robot.name}
      email={robot.email}
      id={robot.id} />
  ));

  return (
    <div>
      {cardsArray}
    </div>
  );
};

PropTypes

  • Tell React that the robots array is required

CardList.propTypes = {
  robots: React.PropTypes.array.isRequired
};
Previousredux-thunkNextredux-merge

Last updated 5 years ago

Was this helpful?