Created
September 12, 2019 17:10
-
-
Save goodpic/59abb955a5e40c522d1005bc24907062 to your computer and use it in GitHub Desktop.
React Native Hooks example to test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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