GithubHelp home page GithubHelp logo

rerails-client's Introduction

Client component of React-Rails Todo App

If you like it, give this project some love by giving it a star!

Overall this is a simple todo-app that uses Rails backend and React frontend. Perfect to learn how to communicate between two different projects.

ReRails Demo

This part will cover the Client part. To see the backend/ API part, check out the rerails-API repo.

Setting up React project

To create this project, I used create-react-app. Once done, npm run build to configure build environment. npm run start will launch the app at, by default, localhost:3000. Before we start, inside package.json, add "proxy": "http://localhost:3001/". This will tell our app to "proxy" at localhost:3001 to get our data. We will also configure our Rails API to send data to localhost:3001.

How to setup React to get data from Rails API Source (works on most API sources)

I will leave out the UI/ Bootstrapping part because that is not crucial to understand the big cheese. The big take-home I want you to have is to be able to connect React and Rails.

To GET the API data from todo file, we will use fetch method. Checkout fetch API here.

I created a directory to store all fetching that we will do, calling it Client.js. Feel free to take a peek at that.

function getTodos(cb) {
  return fetch(`api/todos`,  {
      headers : {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }})
    .then((response) => response.json())
    .then(cb);
};

The getTodos takes one argument, the callback argument. The callback argument will be used to set state todos. Check my Todos.js for more info. Essentially, when Client.getTodos() is run, it will fetch all the data (with promise), gets all the todos data in an array, and once we get them, we will set them to equal our this.state.todos. That's it!

Now that this.state.todos contain all todos, we can map them and display them as a list. I created a new component called TodoList.js to do that. I have something different on my todolist directory, but essentially you just need something like:

{this.props.todos.map((todo, index) =>
  <li key={index}>{todo.description}</li>
)}

The rest of the requests, postTodos() and deleteTodo is left as an exercise. They are very similar to getTodos(), except they use POST and DELETE method, respectively. The codes are on this repo. You can find the postTodo() in action inside AddTodo.js and deleteTodo inside TodoList.js.

If you have any question, feel free to let me know!

rerails-client's People

Stargazers

Igor Irianto avatar Alexsey Ramzaev avatar

Watchers

James Cloos avatar Igor Irianto avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.