Skip to content

Commit aeead46

Browse files
committed
docs(react): show complete code context in the "Your FirstApp" tutorial
1 parent d4c5cda commit aeead46

File tree

6 files changed

+1104
-224
lines changed

6 files changed

+1104
-224
lines changed

docs/react/your-first-app.md

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,22 @@ After installation, open up the project in your code editor of choice.
102102
Next, import `@ionic/pwa-elements` by editing `src/main.tsx`.
103103

104104
```tsx
105+
import React from 'react';
106+
import { createRoot } from 'react-dom/client';
107+
import App from './App';
108+
// CHANGE: Add the following import.
105109
import { defineCustomElements } from '@ionic/pwa-elements/loader';
106110

107111
// Call the element loader before the render call
108112
defineCustomElements(window);
113+
114+
const container = document.getElementById('root');
115+
const root = createRoot(container!);
116+
root.render(
117+
<React.StrictMode>
118+
<App />
119+
</React.StrictMode>
120+
);
109121
```
110122

111123
That’s it! Now for the fun part - let’s see the app in action.
@@ -147,10 +159,12 @@ Open `/src/pages/Tab2.tsx`. We see:
147159
<IonTitle>Photo Gallery</IonTitle>
148160
```
149161

150-
We put the visual aspects of our app into `<IonContent>`. In this case, it’s where we’ll add a button that opens the device’s camera as well as displays the image captured by the camera. Start by adding a [floating action button](https://ionicframework.com/docs/api/fab) (FAB). First, update the imports at the top of the page to include the Camera icon as well as some of the Ionic components we'll use shortly:
162+
We put the visual aspects of our app into <ion-content>. In this case, it’s where we’ll add a button that opens the device’s camera as well as displays the image captured by the camera. Start by adding a [floating action button](https://ionicframework.com/docs/api/fab) (FAB) to the bottom of the page and set the camera image as the icon.
151163

152164
```tsx
165+
// CHANGE: Add the following import.
153166
import { camera, trash, close } from 'ionicons/icons';
167+
// CHANGE: Add the following import.
154168
import {
155169
IonContent,
156170
IonHeader,
@@ -166,22 +180,33 @@ import {
166180
IonImg,
167181
IonActionSheet,
168182
} from '@ionic/react';
183+
import ExploreContainer from '../components/ExploreContainer';
184+
import './Tab2.css';
185+
186+
const Tab2: React.FC = () => {
187+
return (
188+
<IonPage>
189+
<IonHeader>
190+
<IonToolbar>
191+
<IonTitle>Tab 2</IonTitle>
192+
</IonToolbar>
193+
</IonHeader>
194+
<!-- CHANGE: Add the floating action button. -->
195+
<IonContent>
196+
<IonFab vertical="bottom" horizontal="center" slot="fixed">
197+
<IonFabButton onClick={() => takePhoto()}>
198+
<IonIcon icon={camera}></IonIcon>
199+
</IonFabButton>
200+
</IonFab>
201+
</IonContent>
202+
<!-- END OF CODE BLOCK -->
203+
</IonPage>
204+
);
205+
};
206+
207+
export default Tab2;
169208
```
170209

171-
Then, add the FAB to the bottom of the page. Use the camera image as the icon, and call the `takePhoto()` function when this button is clicked (to be implemented soon):
172-
173-
```tsx
174-
<IonContent>
175-
<IonFab vertical="bottom" horizontal="center" slot="fixed">
176-
<IonFabButton onClick={() => takePhoto()}>
177-
<IonIcon icon={camera}></IonIcon>
178-
</IonFabButton>
179-
</IonFab>
180-
</IonContent>
181-
```
182-
183-
We’ll be creating the `takePhoto` method and the logic to use the Camera and other native features in a moment.
184-
185210
Next, open `src/App.tsx`, remove the `ellipse` icon from the import and import the `images` icon instead:
186211

187212
```tsx
@@ -191,10 +216,47 @@ import { images, square, triangle } from 'ionicons/icons';
191216
Within the tab bar (`<IonTabBar>`), change the label to “Photos” and the `ellipse` icon to `images` for the middle tab button:
192217

193218
```tsx
194-
<IonTabButton tab="tab2" href="/tab2">
195-
<IonIcon icon={images} />
196-
<IonLabel>Photos</IonLabel>
197-
</IonTabButton>
219+
// Keep other imports
220+
// CHANGE: Add the following import.
221+
import { images, square, triangle } from 'ionicons/icons';
222+
223+
const App: React.FC = () => (
224+
<IonApp>
225+
<IonReactRouter>
226+
<IonTabs>
227+
<IonRouterOutlet>
228+
<Route exact path="/tab1">
229+
<Tab1 />
230+
</Route>
231+
<Route exact path="/tab2">
232+
<Tab2 />
233+
</Route>
234+
<Route path="/tab3">
235+
<Tab3 />
236+
</Route>
237+
<Route exact path="/">
238+
<Redirect to="/tab1" />
239+
</Route>
240+
</IonRouterOutlet>
241+
<IonTabBar slot="bottom">
242+
<IonTabButton tab="tab1" href="/tab1">
243+
<IonIcon aria-hidden="true" icon={triangle} />
244+
<IonLabel>Tab 1</IonLabel>
245+
</IonTabButton>
246+
<!-- CHANGE: The label to `Photos` and the `ellipse` icon to `images` -->
247+
<IonTabButton tab="tab2" href="/tab2">
248+
<IonIcon icon={images} />
249+
<IonLabel>Photos</IonLabel>
250+
</IonTabButton>
251+
<IonTabButton tab="tab3" href="/tab3">
252+
<IonIcon aria-hidden="true" icon={square} />
253+
<IonLabel>Tab 3</IonLabel>
254+
</IonTabButton>
255+
</IonTabBar>
256+
</IonTabs>
257+
</IonReactRouter>
258+
</IonApp>
259+
);
198260
```
199261

200262
:::note

0 commit comments

Comments
 (0)