Is there a way to make React Native custom components without UI? - react-native

I am writing a custom component for a Windows 8.1 tablet application our team are mostly developing in React Native.
I've realised that some of the custom code we need doesn't naturally belong to any specific UI element. For example, we want a button to trigger the native Camera UI dialog (as in this CameraUICapture element sample). However, there's no reason this would necessarily be triggered from a button. It could be a callback from something else, it could be a click event on an image. I don't want to lock the function calls to a specific UI piece.
All the tutorials and demos I have managed to find so far for React Custom Components are explicitly for UI pieces, and require implementing a React View manager subclass to interact with React when the piece is loaded. Is this the only way to write native code accessible from a React page? Do I need at least a dummy UI element even to hook into functional code in the native layer, or is there another way?

It turns out the phrase I'm yearning for is "Native Modules".
iOS:
https://facebook.github.io/react-native/docs/native-modules-ios.html
Android:
https://facebook.github.io/react-native/docs/native-modules-android.html
Windows:
https://github.com/Microsoft/react-native-windows/blob/master/docs/NativeModulesWindows.md

Related

Getting the current state of transition in #react-navigation/native-stack

I am developing a library consisting of React Native components and this library is used by a React Native application. I have a problem where I want to avoid rendering WebViews during transitions since it is causing crashes on Android. My problem occurs when the application switches to a new screen and renders a component from this component library. The component adds listeners for transitionEnd and beforeRemove. And the problem is that the transitionEnd listener doesn't always seem to get initialised soon enough for me to be able to recognise that the transition has ended and I am not able to determine if I can safely render a WebView.
Do you see a solution that already exists in react-navigation or is this perhaps a feature that should exist?

Need to create a toggle component using switch which can be reused

I want to create a Toggle component but it should be created in such a way that it can be used in various modules, please let me know how can I do this.
Plenty of ways to do so. You can use ready modules like react native paper or react native switch or if you want your own implementation you can use react native gesture handler and handle clicks and gestures on your own with a couple of views.

Why Should I Adopt React Native in Old Projects?

I've been digging deep in React Native for the past week to decide if I and the team should adopt it. And I gotta say it seems promising. I've also learned about its ability to integrate with existing native projects and its incremental adoption to change existing native projects.
My question is what are the cases/reasons that would make me wanna convert my completed old native projects (iOS/Android) into React Native?
Edit: Just to clarify, our team is currently building native apps only.
It depends on what type of app you are building. But you can always use bits of code from native or react native. Since you all can code in native, anything you cannot find you could make a native component.
The advantages of React Native show up with code reuse between iOS and Android so that your team can iterate faster working together. Many big apps have achieved 80% or more code reuse.
You will find flexbox so easy to quickly create views to specs and to change quickly with styles. Plus they are all in code and that plays nice with git.
See edit below:
Now in saying this I have never used React Native inside a native project but to call actions and such you would need to expose functions to call from the JavaScript side. I don't know if a singleton is generally used or just an import of the controller functions or delegated you want to expose but it has to call back to the native application . It is easy to use a native component inside a React native application and I have done this so I am sure it can work the other way.
Edit: Out of curiosity I added some react native views to a test project and it works surprisingly well. No singleton needed Javascript maps the view tag and the exported functions. Crazy. The concerns below still exist but I think if you are changing to change that is wrong but if you have UI changes you want to make then it could definitely benefit as your reuse can grow across your platforms.
Drawbacks I have seen- sometimes transition animations can be laggy if you are not using the native navigation methods. Using both native and React Native adds size to your app so you would need to keep an eye on that. Also start up time can hurt and so monitor that as well.
With all that said I have built a fair bit with React Native lately and it is, at times, beautiful and frustrating for a native developer.
Here is a great article on Instagram and React Native.
Also AirBnb heavily uses RN. They may be completely RN now but I am not sure. They do drive a lot of the outside frameworks in RN for example the maps component in RN. They also just released Lottie and it is very cool. Here is an article about AirBnb.

React Native Navigation Problems

I am new to React Native and want to be able to make my code where the user clicks on text and it takes them from an initial page and am very confused as to how I am supposed to do this and where to put what. Am I supposed to have separate files for separate pages? Thanks!
The Navigator or NavigatorIOS components are what you're looking for. Basically, your text elements need to be wrapped in an element like TouchableHighlight that has an onPress handler that utilizes the navigator.push() to navigate. Or you could jThis tutorial does a pretty good job describing how Navigation works on a basic level within React Native apps.
As to separate files for separate pages - it's not necessary, but as you develop your own custom components and your apps become more complicated, having them in separate files helps with keeping you

Using React Native within existing iOS app for some views only

Is it possible to use React Native only for one view within the project?
I've successfully added React view for particular iOS app screen (using instructions from "Integration with existing iOS project" docs), but i don't know how to get data from that screen and call other (objective-c) code. For example I want to replace old storyboard-based Search Form to React view and then call storyboard-based screens when user clicks "Search".
Or it is intended to make all views in React and convert existing non-React views into 'native' components (very huge work for big apps)?
This is easily accomplished via the Native Module concept which is described here - https://facebook.github.io/react-native/docs/nativemodulesios.html
You can export obj-c methods to JS via a single call.
I've solved my issue in following way:
Create Native Module wrapper around NSNotificationCenter, so
javascript code could publish iOS events.
Subscribe for that event within ReactController-wrapper around React-Native code.
Raise event when user clicked «Search» (when we need to give control back to objective-c) with needed data to pass as dictionary.
Catch event, process data, open other controllers/etc