Home > Web Front-end > JS Tutorial > Connecting frontend And Backend In react.js

Connecting frontend And Backend In react.js

Mary-Kate Olsen
Release: 2025-01-05 22:12:43
Original
339 people have browsed it

Connecting frontend And Backend In react.js

Connecting React.js Frontend to Backend

create a fresh folder
run the command in the folder directory:-
npm create vite@latest
cd to the given project-name
and run npm install
Copy after login
import { useState ,useEffect } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import axios from 'axios'
function App() {
   const [data,setData]=useState({});

   useEffect(()=>{
     const fetchData=async ()=>{
        const result=await axios.get("https://jsonplaceholder.typicode.com/posts/1");
        setData(result.data);
     }

     fetchData()
   },[])

   return <div>
     {data ? JSON.stringify(data) : "Loading..."}
   </div>
}

export default App

Copy after login

The above is the detailed content of Connecting frontend And Backend In react.js. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template