r/reactnative • u/wallpunch_official • 15h ago
Can I create a native module for Mac app developed in React Native?
I'm trying to develop a cross-platform (iOS, Android, Mac, Windows) app in React Native. Part of the app functionality requires calling native APIs from the Javascript code.
For iOS, I have successfully used the guide here to create a Turbo Native Module in the project.
When I follow the same steps for Mac (same guide as above but adding the "npx react-native-macos-init" step from here), the project builds successfully but Metro gives an error:
TurboModuleRegistry.getEnforcing(...): 'NativeLocalStorage' could not be found. Verify that a module by this name is registered in the native binary.
Has anyone successfully added a native module to a React Native Mac project? Or know of resources I could use to learn how to do so?
1
u/eablokker 14h ago
I have been building a component library for react native macos. It is not completed and not public yet. I have made many native components for macos but they are using the legacy architecture. I have not made any using turbo native modules.
Have you gotten react native macos working without the custom module? Is your module named "NativeLocalStorage"? It says it couldn't be found in the native binary, so I'm thinking that the codegen step needs to be run. In that documentation they only included instructions for ios. You may need to run the pod install from the macos folder, and maybe configure something in the codegenConfig in package.json. See the codegen config docs. You may need to add a "macos" key. I'm not sure what to do here because I've never used codegen, but surely you need to configure this to build your macos files correctly.
Also in your C code you'll need references to "NativeLocalStorage", like
RCT_EXPORT_MODULE(NativeLocalStorage)
is an important one.