I have a project in React Native, the server is written in node.js, url, url2, url3, etc. these are the addresses of processed requests to the server. I need the application to detect ipv4 networks when it starts, but I couldn’t do it. Everything is in typescript, but the file with the url is in javascript.
const IP_ADDRESS = "192.168.1.15";
export const url = `http://${IP_ADDRESS}:3000/register/email`;
export const url2 = `http://${IP_ADDRESS}:3000/register`;
export const url3 = `http://${IP_ADDRESS}:3000/news?category=sports`;
export const url4 = `http://${IP_ADDRESS}:3000/login`;
export const url5 = `http://${IP_ADDRESS}:3000/api/profile`;
export const url6 = `http://${IP_ADDRESS}:3000/user`;
Below is the code of the page where url3 is used
import React, { useState, useEffect } from "react";
import { View, Text, FlatList, StyleSheet, Image, TouchableOpacity } from "react-native";
import axios from "axios";
import { Link } from "expo-router";
import { url3 } from "src/app/ip/config";
import { i18n } from "languages/i18n";
import { SvgUri } from "react-native-svg";
const News = () => {
const [news, setNews] = useState([]);
useEffect(() => {
const fetchNews = async () => {
try {
const response = await axios.get(url3);
setNews(response.data.articles);
} catch (error) {
console.error("Ошибка при получении новостей:", error);
}
};
fetchNews();
}, []);
const CabinetIcon = () => {
return (
<SvgUri
width="40px"
height="40px"
uri="https://www.svgrepo.com/show/485646/cabinet1.svg"
/>
);
};
const FilterIcon = () => {
return (
<SvgUri
width="40px"
height="40px"
uri="https://www.svgrepo.com/show/509927/filter.svg"
/>
);
};
return (
<View>
<View style={styles.container}>
<TouchableOpacity onPress={() => {}}>
<Link href="/profile">
<CabinetIcon />
</Link>
</TouchableOpacity>
<Text style={styles.title}>{i18n.t("pageTitle")}</Text>
<TouchableOpacity onPress={() => {}}>
<Link href="/filter">
<FilterIcon />
</Link>
</TouchableOpacity>
</View>
<FlatList
data={news}
keyExtractor={(item) => item.title}
renderItem={({ item }) => (
<View className="border-red-950 border-2 border-solid p-4 ">
<Text>{item.title}</Text>
<Text>{item.description}</Text>
<Link href={item.url}>{i18n.t("newsLink")}</Link>
</View>
)}
/>
</View>
);
};
export default News;
const styles = StyleSheet.create({
container: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
paddingHorizontal: 20,
marginBottom: 20,
},
title: {
fontSize: 24,
fontWeight: "bold",
},
image: {
width: 50,
height: 50,
},
Title: {
fontFamily: "Montserrat",
color: "purple",
fontSize: 24,
},
Icona: {},
});
I’ve already tried to use
import { NetworkInfo } from "react-native-network-info";
// Get IPv4 IP (priority: WiFi first, cellular second)
NetworkInfo.getIPV4Address().then(ipv4Address => {
console.log(ipv4Address);
});
But either I’m stupid or this method doesn’t work.