简体   繁体   中英

How to connect React native with django rest api

I build a simple django rest api now i need to fetch data from it in frontend using React Native. I can make a request to the api but the data is not coming from there. I search about it a lot but didn't find any help Thanks in advance...

npm install axios

const axios = require('axios')

export const App = () =>{
const Your_Request = () =>{
const config= {
method:'get',
url:'YOUR URL',
}
axios(config).then((response)=>{
console.log('response',response)
}).catch((error)=>{
console.log('error',error)})
}
return (<View></View>)
}

You should first try to see whether your API works correctly. I can recommend you Postman ( https://www.postman.com/ ) for that. It's free, comes with an intuitive GUI, and allows you to make requests to your API, see the returned data, status codes, headers, and so on.

Once the API returns the data correctly, you'll need to fetch the data in the React application. For that you can either place fetch() requests in the respective lifecycle methods, although this will get burdensome quite quickly if you need authentication, etc.. Another option would be to build service infrastructure using React hooks to share state (such as API tokens) across the component tree. Here's a post I wrote on how to do that: https://schneider-lukas.com/blog/react-connect-rest-api .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM