Mocking requests in Storybook React Native - react-native

I would like to be able to setup stories for my react native screens, which hooks into the API via react query hooks. I'm using the beta version of storybook react native (6.0.1-beta3).
https://storybook.js.org/addons/msw-storybook-addon, which is what I'd normally use in a react project, won't work because service workers don't exist in react native (if I'm understanding things correctly).
I have also tried installing https://storybook.js.org/addons/storybook-addon-mock/, but wasn't able to get it work (storybook failed with a runtime error when trying to register the plugin... likely a version mismatch).
I might just have to go with a container/dumb component approach for the screens, but that has it's own drawbacks because queries or mutations can be nested in components.
I'm using axios for what it's worth. I've also considered putting the axios instance in a provider so I could modify the instance per test.
Thanks

Related

Video Calling in Expo React Native Application

I am building a React Native Application using Expo and I want to integrate a 1:1 video calling functionality in the app.
From what I have researched so far on the topic is that I can use SDKs of various libraries like Twilio, Videosdk, VoxImplant etc to implement that feature or I have to use WebRtc in native project alongwith some mechanism to create rooms using socket.io and node and then join users in that room (not completely sure about it but something like this)
But both of these solutions require me to make changes in native files which are not present in expo app by default for which I think I have to run expo run:android and then make require changes in files (correct me if I am wrong)
Although on web I think its relatively easy to implement video calling using vanilla js or react js.
My question is if I implement a webpage that has video calling function and try to open it in webview in my expo react native app will the functionality work on app or not? has someone tried this before.
As I was exploring options I came BigBlueButton APIs and another question on Stackoverflow that is using Webview to connect to BigBlueButton APIs. Can I use this logic to implement something in expo app without ejecting or using any sdks? Will it work
What would be the best way to implement video calling in my expo app
Thanks
Utilizing Expo, you are essentially using 'React Native for Web', and with the new functionality of Expo config-plugins, almost all packages for React Native with auto-linking can be made to work with your Expo app, or more-or-less can have Config Plugins created for them.
For your case, good news for you, you can make it all work on Expo managed workflow, just utilize expo-dev-client and the following library:
react-native-webrtc
There's an Expo config plugin for this package. So now all you have to do is just utilize the Web-based functionality of WebRTC, such as calling:
navigator.mediaDevices.getUserMedia()..
navigator.mediaDevices.enumerateDevices()..
navigator.*
And ensure it works properly on Web. Then, just make a platform hook at your App.js / First loaded module / before having these WebRTC based functionalities calling the aforementioned library's initializer. e.g:
import React, { useEffect } from "react";
import { Platform } from "react-native";
import { registerGlobals } from "react-native-webrtc";
...
Platform.OS !== "web" && useEffect(() => {
registerGlobals();
}, []);
...
One more thing you'd have to do is that for Web, have the <video> element and for native apps, resolve it to <RTCView>, so essentially you could also make a platform specific component/module that resolves to either the web-only <video> tag or the native <RTCView> based on platform, like the example above. Perhaps even have the imports be resolved based on dependencies if you face any errors importing 'registerGlobals' at Web, for example.

React native metro config for multi-package workspace

I'm trying to create an example React Native application for my package (https://github.com/BenJeau/react-native-draw) which now is divided in multiple npm packages. Everything is working with Expo, but since I am introducing non-Expo compatible packages, I need to switch the example to a pure React Native project (which can be found in the feat-skia branch).
I inspired myself from #react-navigation repository layout and it worked for the Expo example, but now that I'm moving to a pure React Native example, it is not working either giving one of the following errors variations when loading components from the packages within my library:
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:
Invariant Violation: View config getter callback for component 'RNSVGPath' must be a function (received 'undefined').
Would anyone know how to properly configure metro or have links on setting up metro in a workspace in a pure React Native project?
Thanks!

Why do I need to install redux along with react-redux?

If react-redux depends on redux and can't function without it, why would redux not be part of react-redux library?Is it only because someone may want to uninstall react-redux and still want to keep with redux in a react project? Or is there some other logic?
Because Redux is a standalone JS library that can be used with any UI framework (React, Angular, Vue) or even vanilla JS / jQuery. React-Redux is the specific bindings library that lets your React components interact with the Redux store, and there are bindings libraries for other UI layers as well. So, there are separate but related libraries that do different jobs.
Please see my post The History and Implementation of React-Redux and my talk A Deep Dive into React-Redux for more details on what React-Redux does and how it works.

Boilerplate code for setting up Redux on a Expo/CNApp project?

I've used Ignite CLI to bootstrap a simple react native project, and everything from sample components to Redux were set up and ready to go. What's missing from the Ignite project is the ability to use Expo out of the box. So I though I'd look into Expo and Create React Native App, and see if those provide a similar easy out-of-the-box Redux setup.
So does anyone know of any Expo/CNApp boilerplate code for bootstrapping a react-native project, which includes stuff like Redux and sample screens and components?
Expo offers a New Project Template. While it includes fonts, navigation, icons, app loading, push notifications, and some basic screens, it does not include Redux.
There are good Redux examples from Redux.org. I also found this repo and the associated tutorial to be good a example of how to integrate Redux into a React Native app.

How to make a library to work in React Native environement?

I'm new in React Native, please bear with me. I have a library, which works fine in NodeJS and in browser. I would like to make it usable in React Native too.
I created an example project, imported the library but it threw an exception - Unable to resolve module `http`. If I try to import the browser version, document is not available.
Since then I'm scratching my head - how am I supposed to make my library to work in React Native if neither the core NodeJS modules, nor document are available?
React Native does not have access to Node.js modules such as http, so any code that relies on that functionality is not going to work. You will have to remove or replace it. As for document, window and other DOM-specific APIs, RN does not use a DOM at all, instead it uses its own rendering mechanism that is coupled to the native APIs. You will also have to remove all those calls and replace them with React Native-compatible ones.