Using Context API & ReactFire with Ionic Framework & Capacitor Wrap Up

Overview

To wrap up this series we are going to do the following

  • Demonstrate a pattern I use for passing data into a Page to use the same components for creating and editing an object.
  • Managing Default Values with React Hook Form & Ionic React Components, React Hook Form is a great library that simplifies forms in ReactJS
  • Updating Data in Firebase, Firestore using ReactFire; added the functionality to the Context we introduced in the last post.

For brevity, I have only included snippets of code here, the full source code is available in the github project linked at end of post

Create and Update Modal Pattern

is modified to receive the , this is how we will use the same modal for editing and creating new objects.

<AddSomethingModal
initialData={showModal.initialData}
onCloseModal={(data: IModalResponse) => addSomething(data)}
/>

Modified state for showing the to have an additional property for data, which is passed in if there is an object to edit

// manages the state to determine if we need to open
// the modal or not
const [showModal, setShowModal] = useState<{
show: boolean;
initialData?: IModalData;
}>({ show: false });

React Hook Form provides an easy way to set defaultData and it also has a provider to get access to the required functions to properly create components to structure your forms better.

Default Data — https://react-hook-form.com/api/#useForm
useFormContext/FormProvider — https://react-hook-form.com/api/#useFormContext

What we do when editing…
1) Pass data into using the same state object but now including the initialData values,

// Home.tsx
const editSomething = (item: IModalData) => {
setShowModal({ show: true, initialData: item });
};

2) Use the hook with the data passed in

// AddSomethingModal.tsx
const methods = useForm({ defaultValues: initialData });

3) The modal is wrapped with the ReactHookForm and the methods, properties associated with the form are passed as parameters. This give us access to the information in the child components without passing properties down through the component hierarchy.

<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(addTheThing)}>
<MyIonTextItem
labelName="Podcast Name"
name="podcastName" />
</form>
</FormProvider>

4) Access default values in my custom component, since I set the default values when creating the form, the default values will be matched to the element with the matching name when rendered in

// MyIonTextItem.tsx
const { control, errors, register } = useFormContext();
...
<IonItem>
<IonLabel>{labelName}</IonLabel>
<Controller
render={({ onChange }) => (
<IonInput
type="text"
name={name}
ref={register}
onIonChange={onChange}
/>
)}
control={control}
name={name}
rules={{
required: labelName + " is a required field",
}}
/>
</IonItem>

Changes to function where we determine if there is an id, then we will update the item in the database if not, we will add the item

const addSomething = async (response: IModalResponse) => {
setShowModal({ show: false });
if (!response.hasData) {
showAlert("User Cancelled", true);
return;
} else {
try {
if (response.data?.id) {
await updateItem(response.data!);
} else {
await addItem(response.data!);
}
showAlert("Success");
} catch (error) {
showAlert(error.message, true);
}
}
};

Firebase update needed in the file to exposed the new function. Since we are using typescript lets add it to the interface first.

interface IState {
dataCollection: null | undefined | any;
// NEW function for updating items
updateItem: (itemData: IModalData) => Promise<void>;
addItem: (itemData: IModalData) => Promise<void>;
removeItem: (itemData: IModalData) => Promise<void>;
}

Now lets create the function…

const updateItem = (itemData: IModalData) => {
return thingsRef
.doc(itemData.id)
.set({ ...itemData }, { merge: true });
};

Finally lets include it in the data context

// the store object
let state = {
dataCollection: data,
addItem,
updateItem, // <-- NEW
removeItem,
};
// wrap the application in the provider with the initialized context
return <DataContext.Provider value={state}>{children}</DataContext.Provider>;

New Code for rendering the list with the Line component extracted and all the functions passed in.

The new functionality of editing an item is triggered by clicking on the item in the list.

// Home.tsx
<IonList>
{dataCollection.map((e: any) => {
return (
<Line
item={e}
key={e.id}
edit={editSomething}
remove={removeSomething}
/>
);
})}
</IonList>

We created a separate stateless component that just renders the line items and calls appropriate functions when the line is clicked or the delete button on the line is clicked

// Line.tsx
const Line: React.FunctionComponent<{
item: IModalData;
edit: (e: any) => void;
remove: (e: any) => void;
}> = ({ item, edit, remove }) => {
return (
<IonItem>
<IonLabel className="ion-text-wrap" onClick={() => edit(item)}>
<pre>{JSON.stringify(item, null, 2)}</pre>
</IonLabel>
<IonButton onClick={() => remove(item)} slot="end" color="danger">
<IonIcon icon={removeCircleOutline} />
</IonButton>
</IonItem>
);
};
export default React.memo(Line);

Source Code

https://github.com/aaronksaunders/ionic-react-hook-form-react-fire

DC based software agency utilizing #Javascript, #Typescript, #HTML5, #Ionicframework, #NodeJS, #Firebase to build web & mobile solutions. https://buff.ly/300Zru

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store