According to the Expo docs, you cannot run Firebase Analytics alongside Expo Go. I think this is misleading, because in my experience, you can get Analytics running, you just can’t call it from within Expo Go. You can call Analytics from apps built with eas, which is likely what you are running in production anyway.
Here are the steps I took to set up Firebase Analytics for iOS. I am not sure if this will work for Android builds.
Installing packages
You will need to install several packages from your command line.
Make sure you select “Enable Google Analytics for this project”. You may have to go through additional setup if you haven’t previously used Google Analytics.
Select the Analytics account, and press Create Project:
Configure your Firebase project
Once the project is created, select it from the dashboard if you aren’t already on the project page.
Now, select the iOS option from “Get started by adding Firebase to your app” page.
There are several steps on the page, but you only need to do the first couple.
Register your app
You can find your Bundle ID in the bundleIdentifier field of the app.json
Download the GoogleService-Info.plist file
Put the file in the root directory of your project. This should probably not be checked into Git, but I don’t think it is too critical.
You can skip the rest of the steps on the page (SDK, initialization)
App Configuration
You need to add a couple of items to your app.json file.
Expo Go cannot load @react-native-firebase/analytics, so create a wrapper file that will conditionally load it if we aren’t running the app in Expo Go. The code below includes a function logLevelComplete, that takes two parameters: level and moves, and logs a custom event called level_complete.
import Constants, { ExecutionEnvironment } from 'expo-constants'
// `true` when running in Expo Go.
const isExpoGo = Constants.executionEnvironment === ExecutionEnvironment.StoreClient
let analytics
if (!isExpoGo) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
analytics = require('@react-native-firebase/analytics').default
}
export async function logLevelComplete(level: number, moves: number) {
if (isExpoGo) {
console.log(
'levelComplete analytics event, level: ',
level,
'moves: ',
moves
)
} else {
await analytics().logEvent('level_complete', { level: level, moves: moves })
}
}
Now, you can log events by calling the functions you create in firebaseWrapper.ts. If you are running in Expo Go, it will log the event to the console, if you are running the app through an eas build, it will send the Firebase events to Firebase.
Running in Expo Go with the simulator
Prior to Expo 49
You can run Expo Go as you normally would, but you will see a warning in your logs that indicates the build is not installed in your simulator, you can ignore this warning.
The expo-dev-client package is installed, but a development build is not installed on iPhone 12 mini.
Launching in Expo Go. If you want to use a development build, you need to create and install one first.
Learn more
Expo 49
In Expo 49, npx expo start will default to development builds rather than Expo Go if expo-dev-client is installed.
To force Expo Go to be used, launch with: npx expo start --go --clear
If you don’t, you will get an error like the one below, and the app won’t launch. You can also switch to Go by pressing s:
› Opening on iOS...
CommandError: No development build (<appname>) for this project is installed. Please make and install a development build on the device first.
Learn more
If you want to run the application natively, and not through Expo Go, I think you will need to build a preview version of the app through the eas command. This takes a while to compile, and doesn’t work for my workflow. I believe these instructions will help you get that set up, but I haven’t tried it: https://docs.expo.dev/develop/development-builds/create-a-build/#create-a-build-for-emulatorsimulator
Running on your device
To test that the Firebase integration is working, create an eas preview build, with a command like: eas build --profile preview --platform ios --clear-cache
Once the build is done, install it on your device. Within your app, do whatever action will trigger the event call you set up.
If you go to the Firebase dashboard, you should see a count in the Users in last 30 minutes section after a minute or two.
The events take up to 24 hours to show up in Firebase, so be patient.
This post assumes you have a working Expo React Native project, and that you are somewhat familiar with contexts, hooks, and state in React Native. I will show code that will manage a list of users, using a database, hooks with state, and a context.
To do the initial setup for SQLite, run:
expo install expo-sqlite
Overview
Set up file for all of the DB queries
Set up a hook to initialize the database
Set up a context for managing users
Use the context in components
Set up DB Queries
I like to keep my queries in a single file, this way, if I ever want to move off of SQLite, or mock out the DB for tests, I can swap out a single file.
Below is my code that will create our db tables, initialize the users db, get users, insert users. In addition, there is function to drop the db tables, which is helpful during development and test.
const db = SQLite.openDatabase('db.db') opens the database named db.db.
The dropDatabaseTablesAsync, setupDatabaseAsync, and setupUsersAsync are asynchronous functions that return a promise. This means that we can call those functions with await. We will call these functions while showing the splash screen, waiting for the tasks to be finished before we move on.
The other functions aren’t asynchronous because we don’t really need to wait for them to finish.
For getUsers, we pass in a function that takes the array that the query returns as its parameter. We will pass in a function that can take the users from the query and set the state.
For insertUser, we pass in a successFunc that will be called after the insert has happened. In our case, we are passing in the function to refresh the users from the database. This way we know that our state will reflect what is in the database.
At the bottom of the file, we are exporting the functions so we can use them in other components.
The useDatabase Hook
When the app starts up, we want to set up the database tables if they haven’t already been setup, and insert some initial data. When working in dev, we may want to drop the existing tables to start clean, so we include a function call for that, which we can comment out in prod.
Here is the code, we put this file in the hooks directory. Hooks are a convenient location for code that can be called from functional components.
// force the state to clear with fast refresh in Expo
// @refresh reset
import React, {useEffect} from 'react';
import {database} from '../components/database'
export default function useDatabase() {
const [isDBLoadingComplete, setDBLoadingComplete] = React.useState(false);
useEffect(() => {
async function loadDataAsync() {
try {
await database.dropDatabaseTablesAsync()
await database.setupDatabaseAsync()
await database.setupUsersAsync()
setDBLoadingComplete(true);
} catch (e) {
console.warn(e);
}
}
loadDataAsync();
}, []);
return isDBLoadingComplete;
}
Some notes on this code:
The component manages its own state (isDBLoadingComplete) to indicate when the database loading is complete.
We put the code within the useEffect function so that it is called when the component is loaded. By including the [] as the second parameter, we only call this function on the initial render.
The await calls will run in order, and will only move on to the next line when the function has returned.
After all of the database setup functions are called and have returned, we will set the state to indicate that the loading is complete. We will be watching for this state value in the App.js to know when we can hide the splash screen and show the homescreen.
The // @refresh reset comment will force the state to be cleared when the app refreshes in Expo.
Initializing the Database
We only want to initialize the database when the application first starts, and since the app can’t really work without the database, we should show the splash screen until the initialization is done. We can do this in the App.js file.
Since the useEffect function within useDatabase is asynchronous, we can’t guarantee that it will return right away. We set it up to track a state flag indicating when it is done, so the code in our App.js will watch for that flag.
Here is the relevant code in the App.js.
import React from 'react';
import { View } from 'react-native';
import * as SplashScreen from 'expo-splash-screen';
import useDatabase from './hooks/useDatabase'
import useCachedResources from './hooks/useCachedResources';
export default function App(props) {
SplashScreen.preventAutoHideAsync(); //don't let the splash screen hide
const isLoadingComplete = useCachedResources();
const isDBLoadingComplete = useDatabase();
if (isLoadingComplete && isDBLoadingComplete) {
SplashScreen.hideAsync();
return (
<View>
...Render the app stuff here...
</View>
);
} else {
return null;
}
}
Some notes in the code
Notice we hide the splash screen with SplashScreen.hideAsync() only when both loading flags are true.
The useCachedResources is part of the Expo boilerplate.
The App may return null a few times before the database and cached resources are done loading.
Context for user data
The app will need to have access the user data from multiple screens.
In the example code, we have 2 tabs:
HomeScreen – showing the list of users, with an input field to add users.
UserListScreen – showing the list of users
Both tabs need to be updated with the new user list when a user is inserted. To do this, we can store the user data and functions in a context: https://reactjs.org/docs/context.html
Contexts shouldn’t be used for all data, but if you need to share data across many components, sometimes deeply nested, it might be a good solution.
// force the state to clear with fast refresh in Expo
// @refresh reset
import React, { useEffect, createContext, useState } from 'react';
import {database} from '../components/database'
export const UsersContext = createContext({});
export const UsersContextProvider = props => {
// Initial values are obtained from the props
const {
users: initialUsers,
children
} = props;
// Use State to store the values
const [users, setUsers] = useState(initialUsers);
useEffect(() => {
refreshUsers()
}, [] )
const addNewUser = userName => {
return database.insertUser(userName, refreshUsers)
};
const refreshUsers = () => {
return database.getUsers(setUsers)
}
// Make the context object:
const usersContext = {
users,
addNewUser
};
// pass the value in provider and return
return <UsersContext.Provider value={usersContext}>{children}</UsersContext.Provider>;
};
Some notes on the code
This will create the Context and Provider. We could have also created a Consumer, but since we are using the useContext function, we don’t need it.
Within the addNewUser and refreshUsers functions, we are making our database calls.
In refreshUsers we are sending the setUsers function, which will allow the query to set our local state.
In addNewUser we are sending the refreshUsers function to refresh our state from the database.
We have a useEffect call to instantiate the users list from the database. We only call this function on the first render.
We are set up to take an initial state through props when we create the UsersContextProvider, but those values are quickly overwritten with the useEffect call. I left the code here for reference.
Setting up the Provider
In order to make the context available to the HomeScreen and UserListScreen, we need to wrap a common parent component in the context Provider. This will be done in the App.js.
import {UsersContextProvider} from './context/UsersContext'
.
.
.
<UsersContextProvider>
< parent of HomeScreen and UserListScreen components goes here>
</UsersContextProvider>
Here is the complete App.js file, which is mostly boilerplate from initializing the Expo app.
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import React from 'react';
import { Platform, StatusBar, StyleSheet, View } from 'react-native';
import * as SplashScreen from 'expo-splash-screen';
import useDatabase from './hooks/useDatabase'
import useCachedResources from './hooks/useCachedResources';
import {UsersContextProvider} from './context/UsersContext'
import BottomTabNavigator from './navigation/BottomTabNavigator';
import LinkingConfiguration from './navigation/LinkingConfiguration';
const Stack = createStackNavigator();
export default function App(props) {
SplashScreen.preventAutoHideAsync();
const isLoadingComplete = useCachedResources();
const isDBLoadingComplete = useDatabase();
if (isLoadingComplete && isDBLoadingComplete) {
SplashScreen.hideAsync();
return (
<View style={styles.container}>
{Platform.OS === 'ios' && <StatusBar barStyle="dark-content" />}
<UsersContextProvider>
<NavigationContainer linking={LinkingConfiguration} >
<Stack.Navigator>
<Stack.Screen name="Root" component={BottomTabNavigator} />
</Stack.Navigator>
</NavigationContainer>
</UsersContextProvider>
</View>
);
} else {
return null;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
}
});
Accessing the context from a component
To access the context, we use the useContext function, passing in our desired context. In the case of the UserListScreen.js, we just need the users, which we then render within our return call.
import React, {useContext} from 'react';
import {StyleSheet, Text} from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';
import {UsersContext } from '../context/UsersContext'
export default function UserListScreen() {
const { users } = useContext(UsersContext)
return (
<ScrollView style={styles.container}>
<Text>Here is our list of users</Text>
{users.map((user) => (
<Text key={user.id}>{user.name}</Text>
))}
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fafafa',
},
});
We do something similar in the HomeScreen.js, but we also import the function to add a new name: addNewUser.
I think this set up will allow me to easily re-use the database related code, without much overhead in each component that needs the data.
I am not certain that having the users and the functions to set the uses in a context is the best approach, but it seems appropriate for my small use case.
I will walk through adding a gradient border to a circular button in React Native. Here is a post on how to create a round button: Round Buttons in React Native
The final code will create a button that looks like this:
This code will generate a button that looks like this:
Add gradient border
We will create 2 circles, one on top of the other. The background circle will be slightly larger and have the gradient applied. The circle in the foreground will be a solid color that will overlap the gradient one, except for on the edges, allowing the gradient to show through. Below is what the background circle looks like.
A couple of notes on the code
<LinearGradient> will apply the gradient, it is available in the expo-linear-gradient module. I haven’t tried it, but it appears you can use this module without Expo. Documentation can be found here: LinearGradient
The start and end props of the LinearGradient specify the angle of the gradient.
The colors specify the different colors to use, I like how 3 looks.
The size of the border is based on a ratio, and set in the gradientRatio function. I just played around with values to get something that I thought looked good.
We will reduce the size of the solid color circle based on the gradientRatio.
We remove the borderWidth from the button style because technically the button has no border now.
The margin of the solid color circle is equal to the half of the difference in circle sizes. This splits the size difference on all sides to center the circle.
I built a simple React Native application that includes round buttons. The design includes 1 large round button and 2 smaller ones nested in the corners.
I ran into an issue where the corners of the containing element respond to clicks, even though it is outside of the circle.
In this post, I walk through creating a circular button where the corners don’t respond to clicks.
TLDR: You can jump to my final solution near the bottom of the page, here.
Problem – The corners can be clicked
My first iteration of the circular button looked fine, but the TouchableOpacity element is a square, and the corners outside of the circle were still clickable. This is fine in smaller buttons where the entire element is a reasonable touch target, but in bigger buttons, the corner areas can be quite large.
As an example, the button below will register clicks in both the blue and orange areas. Ideally, only the blue area would register clicks.
This issue is compounded in my case because I am nesting additional buttons in the corners. This overlap will register big button clicks, when small button clicks are intended.
To start, we create a simple circular button that uses a TouchableOpacity element to register the touches. It will look like this:
The key to making the button round is to include a border radius that is at least 50% of the width and height.
To make it simple, I am passing in a circleDiameter prop that is used to calculate the height, width, and borderRadius. In order for the props to be used in the styles, we need to pass them into the styles as a parameter. I do this through the localStyles variable.
Here is the code for a simple circular button:
import React from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
export default class SimpleCircleButton extends React.Component {
render(){
let localStyles = styles(this.props) //need to load styles with props because the styles rely on prop values
return (
<View style={localStyles.container}>
<TouchableOpacity
activeOpacity={.8} //The opacity of the button when it is pressed
style = {localStyles.button}
onPress = {this.props.onPress}
>
{this.props.children}
</TouchableOpacity>
</View>
)
}
}
const styles = (props) => StyleSheet.create({
container: {
position: 'relative',
zIndex: 0,
backgroundColor: 'rgba(255,95,28,0.42)', //add a background to highlight the touchable area
},
button: {
backgroundColor: 'rgba(20,174,255,0.51)',
justifyContent: 'center',
alignContent: 'center',
borderWidth: 3,
borderRadius: (props.circleDiameter / 2),
width: props.circleDiameter,
height: props.circleDiameter,
},
});
We can then add the circle like this:
//...Other code above
// The `onPress` function will be called when the button is pressed
// The content of the <SimpleCircleButton> will be displayed in the button, in our case, an image that shows "+1".
<SimpleCircleButton
onPress = {() => props.addScore(1)}
circleDiameter = {300}
>
<Image source={ require('../assets/images/plus-1.png') } />
</SimpleCircleButton>
//...Other code below
Using the code above, we get a button like the image below, where the orange and blue areas are clickable. Next we will make the orange area not clickable.
2) Create corner masking
First, we will focus on the top left quadrant of the circle.
To prevent clicking in the orange corner area, we can fill the space with non-clickable elements the have a higher z-index (ios) or elevation (android). We can use a series of rectangles to approximate the area, like in the images below.
This is the equation we can use to calculate the width: width = radius - √(radius2 - height2)
Convert our equation to code
Now let’s update the SimpleCircleButton to include masking rectangles. We will start with 7 rectangles to keep it simple, but we will add more later. The more rectangles we have, the smaller the height of each one, which fits closer to the circle. However, we don’t want to hinder performance by adding too many. I used 13 in my app.
7 bars
50 bars
import React from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
export default class SimpleCircleButton extends React.Component {
constructor(props) {
super(props)
this.numberOfRectangles = 7
// The style used for the rectangles
// the zIndex and elevation of 10 puts the rectangles in front of the clickable button
this.baseRectangleStyle = {
position: 'absolute',
zIndex: 10,
elevation: 10,
}
}
fillRectangle = (iteration) => {
// The radius of a circle is the diameter divided by two
const radius = this.props.circleDiameter / 2
// base the height of each bar on the circle radius.
// Since we are doing 1 quadrant at a time, we can just use the radius as the total height
// Add 1 to the value b/c we will subtract one down below to get rid of the zero index
const barHeight = radius / (this.numberOfRectangles + 1)
// round the radius up, so get rid of fractional units
const roundedRadius = Math.ceil(radius)
// The y value is the height of our bars, * the number of bars we have already included
const y = (barHeight * iteration)
// here is where we apply our modified Pythagorean equation to get our x coordinate.
const x = Math.ceil(Math.sqrt(Math.pow(radius, 2) - Math.pow(y, 2)))
// Now get the width of the bar based on the radius.
let width = roundedRadius - x
// The bar dimensions
const size = {
width: width,
height: barHeight
};
// The bar location. Since we are starting with the top left, we need to add the radius to the y value
let location = {
left: 0,
bottom: y + roundedRadius,
};
// Add some colors to the bars. In our final version we won't do this.
let color = '#FF5F1C'
if(iteration === 5){ color = '#1da1e6' }
// Create a unique key to identify the element
// let key = "" + iteration + starting + color
let key = "" + iteration + color
return(
<View key={key} style={{...this.baseRectangleStyle, backgroundColor: color, ...size, ...location}}></View>
)
};
renderLines = () => {
//start with index+1 b/c 0 will be a width of zero, so no point in doing that math
return [...Array(this.numberOfRectangles)].map((_, index) => this.fillRectangle(index+1))
}
fillRectangles = () => {
return(
<React.Fragment>
{this.renderLines()}
</React.Fragment>
)
};
render(){
let localStyles = styles(this.props)
return (
<View style={localStyles.container}>
<TouchableOpacity
activeOpacity={.8}
style = {localStyles.button}
onPress = {this.props.onPress}
>
{this.props.children}
</TouchableOpacity>
{this.fillRectangles()}
</View>
)
}
}
const styles = (props) => StyleSheet.create({
container: {
position: 'relative',
zIndex: 0,
},
button: {
backgroundColor: 'rgba(20,174,255,0.31)',
justifyContent: 'center',
alignContent: 'center',
borderRadius: (props.circleDiameter / 2),
borderWidth: 3,
width: props.circleDiameter,
height: props.circleDiameter,
},
});
Running our updated code looks like the image below. The colored bars are not clickable, but the round button is. The blue bar is for reference back to our original drawing of bars.
Add bars to other quadrants
Now add the other quadrants.
Increase the numberOfRectangles to 15 to get a bitter circle fit
Add code to the constructor to reduce the math we do for each quadrant * iteration combination
Move the radius
Create a new variable fillRectangleHeight
Add a starting parameter to the fillRectangle. This specifies the quadrant to be displayed.
Add a new set of if statements that will set the location styles, depending upon the quadrant.
Add starting to the unique key
Add starting parameter to renderLines to be passed through to fillRectangle.
Add new calls to renderLines for each quadrant.
import React from 'react';
import {View, StyleSheet, TouchableOpacity } from 'react-native';
export default class SimpleCircleButton extends React.Component {
constructor(props) {
super(props)
//CHANGE VALUE
this.numberOfRectangles = 15 //Define how many rectangles we want
//START NEW CODE
// The radius of a circle is the diameter divided by two
this.radius = this.props.circleDiameter / 2
// base the height of each bars on the circle radius.
// Since we are doing 1 quadrant at a time, we can just use the radius as the total height
// Add 1 to the value b/c we will subtract one down below to get rid of the zero index
this.fillRectangleHeight = this.radius / (this.numberOfRectangles + 1)
//END NEW CODE
// The style used for the rectangles
// the zIndex and elevation of 10 puts the rectangles in front of the clickable button
this.baseRectangleStyle = {
position: 'absolute',
zIndex: 10,
elevation: 10,
}
}
// ADD a new `starting` parameter here to represent the quadrant we are working on
fillRectangle = (iteration, starting) => {
//CODE REMOVED HERE
const barHeight = this.fillRectangleHeight
// round the radius up, so get rid of fractional units
const roundedRadius = Math.ceil(this.radius)
// The y value is the height of our bars, * the number of bars we have already included
const y = (barHeight * iteration)
// here is where we apply our modified Pythagorean equation to get our x coordinate.
const x = Math.ceil(Math.sqrt(Math.pow(this.radius, 2) - Math.pow(y, 2)))
// Now get the width of the bar based on the radius.
let width = roundedRadius - x
// The bar dimensions
const size = {
width: width,
height: barHeight
};
// The bar location. Since we are starting from the middle, working out way out, we need to add the radius to y
// START NEW CODE - depending on the quadrant, change the location
const verticalLocation = y + roundedRadius
let location = {}
if(starting === 'topLeft'){
location = {
left: 0,
bottom: verticalLocation,
};
}else if(starting === 'bottomLeft'){
location = {
left: 0,
top: verticalLocation,
}
}else if(starting === 'topRight'){
location = {
right: 0,
top: verticalLocation,
}
}else if(starting === 'bottomRight'){
location = {
right: 0,
bottom: verticalLocation,
}
};
//END NEW CODE
// Add some colors to the bars. In our final version we won't do this.
let color = '#FF5F1C'
// Create a unique key to identify the element
let key = "" + iteration + starting + color
return(
<View key={key} style={{...this.baseRectangleStyle, backgroundColor: color, ...size, ...location}}></View>
)
};
//START NEW CODE
renderLines = (starting) => {
//start with index+1 b/c 0 will be a width of zero, so no point in doing that math
return [...Array(this.numberOfRectangles)].map((_, index) => this.fillRectangle(index+1, starting))
}
//END NEW CODE
fillRectangles = () => {
return(
<React.Fragment>
{/*START NEW CODE*/}
{this.renderLines('topLeft')}
{this.renderLines('bottomLeft')}
{this.renderLines('topRight')}
{this.renderLines('bottomRight')}
{/*END NEW CODE*/}
</React.Fragment>
)
};
render(){
let localStyles = styles(this.props)
return (
<View style={localStyles.container}>
<TouchableOpacity
activeOpacity={.8}
style = {localStyles.button}
onPress = {this.props.onPress}
>
{this.props.children}
</TouchableOpacity>
{this.fillRectangles()}
</View>
)
}
}
const styles = (props) => StyleSheet.create({
container: {
position: 'relative',
zIndex: 0,
},
button: {
backgroundColor: 'rgba(20,174,255,0.31)',
justifyContent: 'center',
alignContent: 'center',
borderRadius: (props.circleDiameter / 2),
borderWidth: 3,
width: props.circleDiameter,
height: props.circleDiameter,
},
});
Running this new code results in the image below
All 4 quadrants filled in
TLDR: Final Code
Remove some comments and the bar coloring to clean up the code.
import React from 'react';
import {View, StyleSheet, TouchableOpacity } from 'react-native';
export default class SimpleCircleButton extends React.Component {
constructor(props) {
super(props)
this.numberOfRectangles = 15
this.radius = this.props.circleDiameter / 2
// base the height of each bars on the circle radius.
// Add 1 to the value b/c we will subtract one down below to get rid of the zero index
this.fillRectangleHeight = this.radius / (this.numberOfRectangles + 1)
// The style used for the rectangles
// the zIndex and elevation of 10 puts the rectangles in front of the clickable button
this.baseRectangleStyle = {
position: 'absolute',
zIndex: 10,
elevation: 10,
}
}
fillRectangle = (iteration, starting) => {
const barHeight = this.fillRectangleHeight
const roundedRadius = Math.ceil(this.radius)
const y = (barHeight * iteration)
const x = Math.ceil(Math.sqrt(Math.pow(this.radius, 2) - Math.pow(y, 2)))
let width = roundedRadius - x
// The bar dimensions
const size = {
width: width,
height: barHeight
};
const verticalLocation = y + roundedRadius
let location = {}
if(starting === 'topLeft'){
location = {
left: 0,
bottom: verticalLocation,
};
}else if(starting === 'bottomLeft'){
location = {
left: 0,
top: verticalLocation,
}
}else if(starting === 'topRight'){
location = {
right: 0,
top: verticalLocation,
}
}else if(starting === 'bottomRight'){
location = {
right: 0,
bottom: verticalLocation,
}
};
// Create a unique key to identify the element
let key = "" + iteration + starting
return(
<View key={key} style={{...this.baseRectangleStyle, ...size, ...location}}></View>
)
};
renderLines = (starting) => {
//start with index+1 b/c 0 will be a width of zero, so no point in doing that math
return [...Array(this.numberOfRectangles)].map((_, index) => this.fillRectangle(index+1, starting))
}
fillRectangles = () => {
return(
<React.Fragment>
{this.renderLines('topLeft')}
{this.renderLines('bottomLeft')}
{this.renderLines('topRight')}
{this.renderLines('bottomRight')}
</React.Fragment>
)
};
render(){
let localStyles = styles(this.props)
return (
<View style={localStyles.container}>
<TouchableOpacity
activeOpacity={.8}
style = {localStyles.button}
onPress = {this.props.onPress}
>
{this.props.children}
</TouchableOpacity>
{this.fillRectangles()}
</View>
)
}
}
const styles = (props) => StyleSheet.create({
container: {
position: 'relative',
zIndex: 0,
},
button: {
backgroundColor: 'rgba(20,174,255,0.31)',
justifyContent: 'center',
alignContent: 'center',
borderRadius: (props.circleDiameter / 2),
borderWidth: 3,
width: props.circleDiameter,
height: props.circleDiameter,
},
});
The cleaned up code will create a button like this
Final button
Limitations
There are a few limitations to consider
The bars don’t cover 100% of the space outside of the circle, but it is close enough for registering or not registering touch events.
If the number of bars is high, or the button is rerendered a lot, this code may not be super performant. In my production version, I only render the quadrants that are close to other elements that respond to touch. You could add configuration to conditionally render the quadrants based on a prop value
I am building a simple app using React Native and Expo. Many of the guides mention that the Apple Store requires, and Google Play may require, a Privacy Policy and Terms and Conditions.
Problem
I built my Privacy Policy and Terms and Conditions documents into my React Native Expo app, hardcoding the content in a function. It wasn’t until I started the app submission process, that I found, in addition to the policies being required within the app, the stores also ask for a link to the policies.
Setting up the policies in the app was tedious, and I don’t want to manage online policies, as well as in app ones.
My Solution
My original thought was to publish the documents online, and also somehow render the html/markdown for my policies in a webview within the app. This solution would work, but seemed more complex than it needed to be.
Instead, I decided to publish the policies online, and link to them through the app. (This seems obvious, I know.)
My requirements for managing the policies:
Easy to deploy
Only have 1 copy of the policies to keep up to date
Easy to change policies
Easy to regenerate, for a drop in replacement, in case my policy requirements change
A process that I can use for future applications
I opted for GitHub Pages to host my policies.
What I like about GitHub Pages:
The docs live within the app repo
The docs can be in html or markdown, making them easy to update
It is very simple. (Pages uses Jekyll, which I am a little familiar with)
It is free
I trust GitHub
Generating the documents
I used the App Privacy Policy Generator to create a markdown version of the Privacy Policy and Terms and Conditions. I manually added the Expo Privacy Policy to the third party section of the Privacy Policy.
GitHub Pages
The basic setup is easy:
Enable Pages under your existing repo “settings”
Add a docs folder
Add an index.md within the docs folder
Push the docs folder to the master branch
The index.md will be published publicly. In a paid plan, the repo can remain private.
The full instructions are here, GitHub Pages, under the “Project Site” tab.
My policies are saved as privacy.md and terms_and_conditions.md, and are linked from the index.md. When published, the urls will include a .html extension.
NOTE: It seems like build process only triggers on index.md changes. You can find the build status under the “Environments” tab of your repo.
React Native
The React Native code is pretty simple. I created a Settings screen which displays the links to my policies. The links are opened in a WebBrowser.
A nice feature of the WebBrowser is that it doesn’t allow the user to type in different addresses. When determining your Apple Store content rating, you must indicate if the app allows for “Unrestricted Web Access”. Answering “yes” to this question gives your app a “17+” rating. If you are only using the WebBrowser in the way described here, you can answer “no” to this question.
Here is the relevant code within the Settings screen:
Since it doesn’t need to manage its own state, Anchor is a Functional Stateless Component. It will always re-render on a prop change, but it is simple enough, I think that is fine.
Conclusion
Whenever I want to update the policies, all I need to do is update the policy.md and terms_and_conditions.md in my master branch. The users will have access to the updates through the app links.