Skip to content

Instantly share code, notes, and snippets.

@gitmathub
Last active January 2, 2020 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitmathub/faa1a5a56e199af03199650b786cf829 to your computer and use it in GitHub Desktop.
Save gitmathub/faa1a5a56e199af03199650b786cf829 to your computer and use it in GitHub Desktop.
React Prototype Tools

Tools that you might want to use in order to populate and style your React (or any other JavaScript) project.

Fake Data - Faker

Fake address, emails, avatars etc

React jsx code:

import faker from 'faker'

...
<img src={faker.image.avatar()} alt="avatar" />

JSON Fake Data - Json Place Holder

In js file:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json))

In react component class:

import axios from 'axios'

...
async componentDidMount() {
  const response = await axios.get('https://jsonplaceholder.typicode.com/todos/1')
  this.setState({ posts: response.data})
}

result:

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

CSS - Semantic UI

include in html

<link
  rel="stylesheet" 
  href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css"
/>

In react:

<div className="ui container">
  <div className="ui segment">
    <div className="ui form">
      <div className="field">
        <label>Search</label>
        <input type="input" />
      </div>
    </div>
  </div>
</div>

REST API testing

Use an REST API for testing if your requests are working.

import unirest from 'unirest'

it('simple rest query works', async () => {
  const result = await unirest
    .get('http://mockbin.com/request')
    .headers({ 'Accept': 'application/json', 'Content-Type': 'application/json' })
    .send({ "parameter": 23, "foo": "bar" })
    .then((response) => {
      console.log(response.body)
      return response.body
    })
  console.log("result", result)
  expect(result).not.toBeNull()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment