Example of a simple calculator with TurboModules in a React Native app
Android:
iOS:
Navigate to the example
directory:
cd example
Add the rtn-calculator module to the project:
yarn add ../rtn-calculator
Run the following command to generate codegen artifacts for iOS:
cd ..
node example/node_modules/react-native/scripts/generate-codegen-artifacts.js \
--targetPlatform ios \
--path example/ \
--outputPath rtn-calculator/generated/
Return to the example directory and install the rtn calculator module:
cd example
yarn add ../rtn-calculator
If you are working with iOS, install the required CocoaPods dependencies:
cd ios
RCT_NEW_ARCH_ENABLED=1 pod install
If you are working with Android, generate the required codegen artifacts:
cd android
./gradlew generateCodegenArtifactsFromSchema
import RTNCalculator from 'rtn-calculator/js/NativeRTNCalculator';
const App: React.FC = () => {
const [result, setResult] = useState < number | null > (null);
const [x, setX] = useState < string > ('');
const [y, setY] = useState < string > ('');
const handleCompute = useCallback(async () => {
const value = await RTNCalculator?.add(x, y);
}, [x, y]);
}