Quickstart

A quick dive into getting started with Lore

Data Structure: Overview

In this section we'll learn about the data structure Lore uses and then create our Tweet component using mock data. At the end of this section your application will look like this:

Lore's Data Structure

Lore has a particular structure it uses to represent data in your application. To illustrate that, take a look at this example Tweet component.

import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';

export default createReactClass({
  displayName: 'Tweet',

  propTypes: {
    tweet: PropTypes.object.isRequired
  },

  render() {
    const { tweet } = this.props;

    return (
      {/* render tweet */}
    );
  }
});
import React from 'react';
import PropTypes from 'prop-types';

class Tweet extends React.Component {
  render() {
    const { tweet } = this.props;

    return (
      {/* render tweet */}
    );
  }
}

Tweet.propTypes = {
  tweet: PropTypes.object.isRequired
};

export default Tweet;
import React from 'react';
import PropTypes from 'prop-types';

class Tweet extends React.Component {

  static propTypes = {
    tweet: PropTypes.object.isRequired
  }

  render() {
    const { tweet } = this.props;

    return (
      {/* render tweet */}
    );
  }
}

export default Tweet;

This component receives a prop called tweet and then renders that tweet. But before we can do that, we first need to know what the data structure of the tweet looks like.

In Lore, all resources have a data structure that looks like this:

{
  id: 1,
  cid: 'c1',
  state: 'RESOLVED',
  data: {
    id: 1,
    user: 4,
    text: "Where have they been keeping her!?",
    createdAt: "2016-10-17T23:13:07.249Z",
    updatedAt: "2016-10-23T23:13:07.281Z"
  },
  error: {}
}

When Lore retrieves data from an API endpoint, such as /tweets/1, it embeds the attributes from the API response in the data property, and then wraps those attributes in a structure that provides some metadata about that resource.

At first glance, this might seem a little verbose, but each property exists to solve a specific problem, which you can read more about here if you'd like.

For this Quickstart, all you need to know is that the data structure is designed to solve application concerns related to optimistic updates, visual communication, server errors, and detecting when resources have been created - all important elements for providing a good user experience.

For endpoints that return a list of resources, such as /tweets, Lore uses a similar data structure:

{
  state: 'RESOLVED',
  data: [
    {...data in structure displayed above...}
  ],
  query: {},
  meta: {},
  error: {},
}

In this structure, data is an array that contains a list of individual resources, each using the data structure we described previously.

Additionally, this structure also provides metadata about the list such as what query was made to the API, and what the state of that query is (such as whether the data is still being fetched or the query has been resolved).

Next Steps

In the next step, we'll use this structure to add some mock data as we build the Tweet component.