React Native without Expo – how to export from function values obtained by promises?

I have this scenario using pure React Native without Expo: I am currently using @react-native-tethering libraries (wifi and hotspot) from GitHub, and trying to populate inside a text paragraph the wifi’s or hotspot’s IP address.
So I have the App() function in main tsx file like :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
return (
<SafeAreaView style={backgroundStyle}>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<Header />
<View
style={{
backgroundColor: isDarkMode ? Colors.black : Colors.white,
}}>
<Section title="Step One">
Edit <Text style={styles.highlight}>App.tsx</Text> Let's check what IP I
have currently: <Text style={styles.highlight}> {returnIpAddress()}</Text>
</Section>>
</View>
</ScrollView>
</SafeAreaView>
);
}
</code>
<code>function App(): React.JSX.Element { const isDarkMode = useColorScheme() === 'dark'; const backgroundStyle = { backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, }; return ( <SafeAreaView style={backgroundStyle}> <ScrollView contentInsetAdjustmentBehavior="automatic" style={backgroundStyle}> <Header /> <View style={{ backgroundColor: isDarkMode ? Colors.black : Colors.white, }}> <Section title="Step One"> Edit <Text style={styles.highlight}>App.tsx</Text> Let's check what IP I have currently: <Text style={styles.highlight}> {returnIpAddress()}</Text> </Section>> </View> </ScrollView> </SafeAreaView> ); } </code>
function App(): React.JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  return (
    <SafeAreaView style={backgroundStyle}>
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <Header />
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <Section title="Step One">
            Edit <Text style={styles.highlight}>App.tsx</Text> Let's check what IP I
            have currently: <Text style={styles.highlight}> {returnIpAddress()}</Text>
          </Section>>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}

so since that tethering library has Promise functions (getDeviceIp(), getMyDeviceIp()), so I created a function which should pass to Text the current IP of device, like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function returnIpAddress() {
const [IpAddress, setIpAddress] = useState<string | null>(null);
useEffect(() => {
const [stateHotspot, setStateHotspot] = useState<boolean | null>(null);
const [stateWifi, setStateWifi] = useState<boolean | null>(null);
TetheringManager.isWifiEnabled().then(wifiState => {
setStateWifi(wifiState ? true : false);
});
HotspotManager.isHotspotEnabled().then(hotspotState => {
setStateHotspot(hotspotState ? true : false);
});
if (stateHotspot) {
TetheringManager.getDeviceIP().then(wifiIP => {
setIpAddress(wifiIP);
});
} else if (stateWifi) {
HotspotManager.getMyDeviceIp().then(hotspotIP => {
setIpAddress(hotspotIP);
});
} else {
setIpAddress('0.0.0.0');
}
return () => IpAddress;
}, []);
return IpAddress;
}
</code>
<code>function returnIpAddress() { const [IpAddress, setIpAddress] = useState<string | null>(null); useEffect(() => { const [stateHotspot, setStateHotspot] = useState<boolean | null>(null); const [stateWifi, setStateWifi] = useState<boolean | null>(null); TetheringManager.isWifiEnabled().then(wifiState => { setStateWifi(wifiState ? true : false); }); HotspotManager.isHotspotEnabled().then(hotspotState => { setStateHotspot(hotspotState ? true : false); }); if (stateHotspot) { TetheringManager.getDeviceIP().then(wifiIP => { setIpAddress(wifiIP); }); } else if (stateWifi) { HotspotManager.getMyDeviceIp().then(hotspotIP => { setIpAddress(hotspotIP); }); } else { setIpAddress('0.0.0.0'); } return () => IpAddress; }, []); return IpAddress; } </code>
function returnIpAddress() {
  const [IpAddress, setIpAddress] = useState<string | null>(null);
  useEffect(() => {
    const [stateHotspot, setStateHotspot] = useState<boolean | null>(null);
    const [stateWifi, setStateWifi] = useState<boolean | null>(null);
    TetheringManager.isWifiEnabled().then(wifiState => {
      setStateWifi(wifiState ? true : false);
    });
    HotspotManager.isHotspotEnabled().then(hotspotState => {
      setStateHotspot(hotspotState ? true : false);
    });
    if (stateHotspot) {
      TetheringManager.getDeviceIP().then(wifiIP => {
        setIpAddress(wifiIP);
      });
    } else if (stateWifi) {
      HotspotManager.getMyDeviceIp().then(hotspotIP => {
        setIpAddress(hotspotIP);
      });
    } else {
      setIpAddress('0.0.0.0');
    }

    return () => IpAddress;
  }, []);

  return IpAddress;
}

to call it inside <Text></Text> and get back the IpAddress; But I get error about using hooks, even it is a function like :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
</code>
<code>Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem. </code>
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

I tried also to call async () supposing I get asynchronously that IP value but it was not permitted.
In another sample project I made with React Native Expo where I created a component export function I made something similar, and that worked instead…
Maybe in React Native without Expo , does that hook run differently way? What would be the trick to obtain that IpAddress in text? Or maybe I just could have missed something?

Thanks!
Cheers!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật