Skip to content

Instantly share code, notes, and snippets.

@asmyk
Created March 12, 2018 10:36
Show Gist options
  • Save asmyk/980b9bc7b69fbfe6c31da364175fe1cf to your computer and use it in GitHub Desktop.
Save asmyk/980b9bc7b69fbfe6c31da364175fe1cf to your computer and use it in GitHub Desktop.
Example of standard react-native form component with state
import React, { Component } from 'react';
import { StyleSheet, Text, TextInput, View, Button } from 'react-native';
class MyForm extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
email: ""
}
this.handleSendClick = this.handleSendClick.bind(this);
}
handleSendClick() {
// necessary validation
// everything is valid - send form data up
this.props.handleSubmit(this.state);
}
render() {
return (
<View>
<TextInput
onChangeText={(name) => this.setState({ name })}
value={this.state.name}
/>
<TextInput
onChangeText={(email) => this.setState({ email })}
value={this.state.email}
/>
<Button onPress={this.handleSendClick} title="Send" />
</View>
)
}
}
export default MyForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment