将 React.js 前端连接到后端
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
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
以上是在react.js中连接前端和后端的详细内容。更多信息请关注PHP中文网其他相关文章!