Skip to content

Instantly share code, notes, and snippets.

@goodpic
Created September 12, 2019 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodpic/59abb955a5e40c522d1005bc24907062 to your computer and use it in GitHub Desktop.
Save goodpic/59abb955a5e40c522d1005bc24907062 to your computer and use it in GitHub Desktop.
React Native Hooks example to test
import * as React from 'react'
import { useState } from 'react'
import {
NativeSyntheticEvent, Text, TextInput, TextInputSubmitEditingEventData,
TouchableOpacity, View} from 'react-native'
interface IProps {
myMock?: () => void
}
const HookTest = (props: IProps) => {
const [value, setValue] = useState('Default text')
return (
<View>
<TextInput
testID="testInput"
placeholder="Input Text"
value={value}
onChangeText={(text: string) => {
setValue(text)
}}
onSubmitEditing={(e: NativeSyntheticEvent<TextInputSubmitEditingEventData>) => {
setValue(e.nativeEvent.text)
}}
/>
<HookTestSub testValue={value} />
<OnPressComponent {...props} />
</View>
)
}
const HookTestSub = (props: { testValue: string }) => {
return (
<View>
<Text testID="subText">{props.testValue}</Text>
</View>
)
}
const OnPressComponent = (props: IProps) => (
<View>
<TouchableOpacity onPress={props.myMock} testID="onPressComponent">
<Text>Press me</Text>
</TouchableOpacity>
</View>
)
export { HookTest }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment