0

I am working on a web app using ReactJS in which I have created a form and stored the values of all the input fields into state of the app. That is, the state of my App.js file contains values of all the input fields created in Form.js file. I want the information stored in state to be passed on to the backend so that I can process a dataset based on it. How do I add a functionality so that on clicking a submit button everything that's in my app state gets passed on to the backend- say to a text file, or in a json file.

Basically I want to search through a dataset (using Elasticsearch) based on the information provided by a user in the form (using ReactJS).

I am new to React so I don't have much knowledge. I have made the web-app but I need suggestions on how to pass the information that I obtain through the form to a backend so that I can do further work.

VITTHAL BHANDARI
  • 139
  • 2
  • 5
  • 15

4 Answers4

2

How do I add a functionality so that on clicking a submit button everything that's in my app state gets passed on to the backend- say to a text file, or in a json file.

You can make a request for data to use in your application using Axios OR Fetch API. You can consume REST APIs using two of the most popular methods known as Axios (a promise-based HTTP client) and Fetch API (a browser in-built web API).

The fetch() API is an inbuilt JavaScript method for getting resources from a server or an API endpoint. It’s similar to XMLHttpRequest

Axios is an easy to use promise-based HTTP client for the browser and node.js. Since Axios is promise-based, we can take advantage of async and await for more readable and asynchronous code. With Axios, we get the ability to intercept and cancel request, it also has a built-in feature that provides client-side protection against cross-site request forgery.

To know more you can refer this

  • Both these above methods, can be used to submit the data from your front end into the back end, so that the data is stored into the back end, and then you can perform several operations on it according to your requirement.

You can refer several blogs and documentation to know more about this:

Use axios to fetch data from an api in ReactJS

Fetch API for POST Request

Axios

Difference between Axios and Fetch API

Recently, I have also created an application, wherein I am integrating React with SpringBoot application.

In this Application. I have set up router and route, created and submit form, called GET, POST, PUT, DELETE request using axios (have also done with fetch API).

submitBook= event =>{
event.preventDefault();
const book = {

         title:  this.state.title,
         author: this.state.author,
         coverphotoURL: this.state.coverphotoURL,
         isbnNumber: this.state.isbnNumber,
         price: this.state.price,
         language: this.state.language

};

const headers = new Headers();
headers.append("Content-Type", "application/json");

fetch("http://localhost:8080/rest/books",{
method:"POST",
body:JSON.stringify(book),
headers
})
.then(response => response.json())
.then((book) => {
if(book){
this.setState({"show":true, "method":"post"});
setTimeout(() => this.setState({"show":false}),3000);

}
else{
this.setState({"show":false});

}});
this.setState(this.initialState);

};

To view the full code, you can refer my Github Repository

ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • @VITTHAL BHANDARI did you get a chance to go through my answer, looking forward to get feedback from you and if it's helpful, please dont forget to upvote and accept :) – ESCoder Jun 12 '20 at 02:36
  • I have read your answer, your code and the links you've provided. It's definitely useful. I was taking my time to adjust the solution according to my problem. I am thinking of a way to store the user input since I don't really have a "back-end". I am thinking of ways to provide this input further to process my data. I'll let you know what solution I find. – VITTHAL BHANDARI Jun 12 '20 at 10:24
  • @VITTHAL BHANDARI If u dont have a "back-end", you can even create a postman mock server, to run your react application. And do tell the solution which you will find :) – ESCoder Jun 12 '20 at 10:32
  • Is there some way I can get my state (which contains a few search keywords) down to python? because then I can use that data and write a python code to import elasticsearch and create a search based on those keywords? – VITTHAL BHANDARI Jun 12 '20 at 11:28
  • @VITTHAL BHANDARI if u r using axios or Fetch API to post your data from front end(i.e your react app) to backend(using the url which u must have mentioned in the axios or Fetch API), then the data is storing in the backend, from which u can retrieve the data and perform your desired work on the data. – ESCoder Jun 12 '20 at 11:48
  • Your code snippet helped me a lot and it worked. I successfully posted my state data using fetch API. Is there any way I can further use that data ? like, what should I do with it then? I can view the posted data in the network tab of the React Dev Tools. – VITTHAL BHANDARI Jun 14 '20 at 05:25
  • I am thinking of developing a back-end with Django or Flask to integrate with my react app – VITTHAL BHANDARI Jun 14 '20 at 05:36
  • @VITTHAL BHANDARI GREAT :) I have never worked with Django or Flask, so I am not in a position to comment on this, but you can refer to this answer https://stackoverflow.com/questions/41867055/how-to-get-django-and-reactjs-to-work-together. But since now your issue is resolved, and you are able to post the data using fetch API can you now please accept my answer ! – ESCoder Jun 14 '20 at 05:42
  • @VITTHAL BHANDARI And it would be much better if you ask another question related to integrating Django and ReactJS – ESCoder Jun 14 '20 at 05:43
  • @VITTHAL BHANDARI thank u for accepting my answer :) – ESCoder Jun 14 '20 at 06:25
0

You need to look into making XHR or using the fetch API (or axios) to make http requests to the backend API.

lanxion
  • 1,350
  • 1
  • 7
  • 20
0

use axios for calling api urls that you have defined in the backend. you can watch a short tutorial on youtube to get familiar with the basics.

0

You may want to use Fetch API documented in MDN. It handles url request, including REST API to parse data over HTTP, e.g. JSON object etc.

Lance
  • 35
  • 1
  • 9
  • The fetch API is not created by MDN, the fetch API doesn't know about the protocol (it doesn't "follow REST"), and it supports any HTTP payload, not just JSON – Andy Ray Jun 11 '20 at 04:55
  • Thanks for the correction, I have since made the ammendment. – Lance Jun 11 '20 at 05:07