import React, { useState, useEffect } from 'react';
import { View, Text, Button, ActivityIndicator, StyleSheet } from 'react-native';
import { NativeModules } from 'react-native';
const { SampleNativeModule } = NativeModules;
export default function NativeLibraryLinkTest() {
const [nativeValue, setNativeValue] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
fetchNativeValue();
}, []);
const fetchNativeValue = async () => {
setLoading(true);
setError(null);
try {
const value = await SampleNativeModule.getValue();
setNativeValue(value);
} catch (e) {
setError('Failed to load native value');
setNativeValue(null);
} finally {
setLoading(false);
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>Native Library Link Test</Text>
{loading && <ActivityIndicator size="large" color="#0000ff" />}
{!loading && error && <Text style={styles.errorText}>{error}</Text>}
{!loading && !error && nativeValue !== null && (
<Text style={styles.valueText}>Native Value: {nativeValue}</Text>
)}
<Button title="Refresh" onPress={fetchNativeValue} />
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16 },
title: { fontSize: 20, marginBottom: 20 },
valueText: { fontSize: 18, marginVertical: 10 },
errorText: { color: 'red', marginVertical: 10 }
});This React Native component demonstrates how to link and use a native library module.
We import NativeModules and extract SampleNativeModule, which is assumed to be linked natively.
On component mount, we call fetchNativeValue to asynchronously get a value from the native module.
While loading, an activity spinner shows. If an error occurs, a red error message appears. Otherwise, the native value is displayed.
The Refresh button lets the user reload the native value anytime.
This pattern helps verify that native libraries are properly linked and callable from JavaScript.