Skip to content

Commit 784bc57

Browse files
committed
chore: Add examples for hooks usage
1 parent 5502b32 commit 784bc57

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

sample/app/testComponents/AppContainer.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import * as React from "react";
22
import { StyleSheet } from "react-nativescript";
3+
import { Clock, ClockFC, Counter } from "./stateful";
34

45
function AppContainer(){
56
return (
67
<stackLayout style={styles.stack}>
78
<label>Hello world!</label>
9+
<Clock/>
10+
<ClockFC/>
11+
<Counter/>
812
</stackLayout>
913
);
1014
}

sample/app/testComponents/stateful.tsx

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as React from "react";
2+
import { useState, useEffect } from "react";
23
import { RNSStyle } from "react-nativescript";
34
import { Color } from "@nativescript/core";
45

56
export class GameLoop {
67
private readonly subscribers = [];
7-
private loopID: number|null = null;
8+
private loopID: NodeJS.Timeout|null = null;
89

910
constructor(private readonly frameRateMs: number = 1000 / 60){
1011
this.loop = this.loop.bind(this);
@@ -158,7 +159,7 @@ export class Marquee extends React.Component<{ text: string }, { index: number }
158159
// ),
159160

160161
export class Clock extends React.Component<{}, { date: Date }> {
161-
private timerID!: number;
162+
private timerID!: NodeJS.Timeout;
162163

163164
constructor(props) {
164165
super(props);
@@ -207,6 +208,44 @@ export class Clock extends React.Component<{}, { date: Date }> {
207208
}
208209
}
209210

211+
export function ClockFC(){
212+
const [date, setDate] = useState(new Date());
213+
214+
useEffect(() => {
215+
const interval = setInterval(() => {
216+
setDate(new Date());
217+
}, 1000);
218+
219+
return () => {
220+
clearInterval(interval);
221+
};
222+
}, []);
223+
224+
return <textView>{date.toLocaleTimeString()}</textView>;
225+
}
226+
227+
export function Counter(){
228+
const [count, setCount] = useState(0);
229+
230+
return (
231+
<>
232+
<label>{count}</label>
233+
<button
234+
onTap={(eventData) => {
235+
setCount((currentCount: number) => {
236+
const nextCount = currentCount + 1;
237+
console.log(`Increasing count from ${currentCount} -> ${nextCount}`);
238+
239+
return nextCount;
240+
});
241+
}}
242+
>
243+
Increment
244+
</button>
245+
</>
246+
);
247+
}
248+
210249
export class GameLoopTest extends React.Component<{}, {}> {
211250
render(){
212251
return React.createElement(

0 commit comments

Comments
 (0)