简体   繁体   中英

How to connect with PHP file in react native?

I am connecting in react native with php files (Back end) like this:

    componentDidMount(){
    return fetch('http://192.168.1.7:80/Encaissement.php',
    .....}

but the adresse ip is changing every time, so how to get directley current ip adress?(PS: I'm not using localhost because it gets me error with network)

OR how to replace 192.168.1.7 as variable and give it a valeur just in one page (because I have lot of pages and when I need to change ip adress I had to change it in like 15 pages)

To configure just in one site you can create a component and include it in your code when you need to fetch the data.

File GetIp.js

const getIp = () => { return 'http://192.168.1.7:80'; }

export default getIp;

Then when you need it yous just need to import it and use it

import getIp from './GetIp';

...
componentDidMount(){
  fetch(getIp() + '/Encaissement.php');   
}

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