Draftbit is a low-code mobile app building platform that outputs production-grade React Native source code, using Expo’s SDK. Sometimes, a user might want to accomplish something with Draftbit that has not yet been productized by the Draftbit team. In those instances, the user would be able to build those features by utilizing Draftbit’s Custom Code feature. Custom Code is built of a custom component that allows you to use JSX code with your Draftbit app. At times, it utilizes an open-source package. You can also use any open-source package in this type of component.
In a nutshell, here what is covered in this post:
Draftbit app now supports adding a custom component. It is called the Custom Code component and allows to inject custom JSX.
Let us first test out this new feature by displaying a text by writing JSX code.
In the example below, we are creating a custom component with a name of SnapComponent. It uses StyleSheet references to add styles for each component. A StyleSheet is an abstraction similar to CSS StyleSheets.
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
export function SnapComponent() {
return (
<View style={styles.container}>
<Text style={styles.text}>Camera Component</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
text: {
fontSize: 18,
color: '#010101'
}
});
From the above code snippet, you can observe its React Native component or how code in a React Native app looks.
Custom components are not supported by the Web Preview currently when building an app with Draftbit. You can view the result in the Preview tab.
Another way to preview a screen is to use Live Preview on an actual device by scanning the QR code using the Expo Go app, to test the features or functionalities building through custom code. Some external components like using a Camera requires hardware and can be tested only on a real device.
To scan the QR code, click on the Preview button from the top panel.
Here is how the app looks like using the Live Preview mode on an Android device.
Using Custom Code, Draftbit supports adding additional packages that are open source libraries or utilities available by Expo and React Native. The Packages tab allows enabling a new package by adding the correct Package Name in the first input field and keep the Version of the package to latest.
For example, as shown below, we are adding the package expo-camera:
To add a new package, click the ‘+’ button and enter the package name and its version.
To remove a package at any given time, click the ‘-’ button at the end of the row where the package is listed.
💜 We at Draftbit love our Community and are always open for your questions, feedback and feature requests. You will also find more resources to learn and build with Draftbit.
Reach out to us on twitter/@draftbit for more questions.