用本机形式的onSubmit形式

时间:2018-11-21 17:14:08

标签: forms react-native onsubmit

很抱歉,如果这是一个基本问题,但我似乎无法在我的react-native项目中找到替换html中的“ form”标签的东西,所以我不知道在哪里调用“ onSubmit”来发送我的搜索请求,因为react-native中的'Form'标记不接受onSubmit事件。那我该用什么呢? 这是我的代码,只是让您知道它的样子

class SearchBar extends Component{
state = { term: '' }

  onFormSubmit(e) {
     e.preventDefault();
     console.log(this.state.term)
  }
 render(){
   return (
     <View style={styles.viewStyle}>
      <Text style=
       {styles.textStyle}>Search For Sick Pics.</Text>

  //so right here is what i dont know what to wrap my Textinput in, like in a react app we would wrap it in the "<form>" tag

        <TextInput 
        style={styles.inputStyle}
        type='text'
        placeholder='Search for Sick pics'
        value = {this.state.term}
        onChange = {event => this.setState({ term: event.target.value })}
        />   

  </View>
  )
 }
}

这是我的主要组件

     class MainPage extends Component {
      state = { images: [] }

    onSearchSubmit = (term) => {
      axios.get('https://api.unsplash.com/search/collections', {
       params: { query: term },
       headers: {
     Authorization: 
     'Client-ID jfkjasdhlkfjskdljfksdjfljasfasdj;fjaskdj'

    }
  }).then((response) => {
   console.log(response)
  })
}

 render() {
   return (
     <View>
         <SearchBar onSubmitEditing={this.onSearchSubmit}/>

     </View>
)

} }

1 个答案:

答案 0 :(得分:1)

尝试一下:

<TextInput 
  style={styles.inputStyle}
  type='text'
  placeholder='Search for Sick pics'
  value = {this.state.term}
  onChange = {event => this.setState({ term: event.target.value })}
  onSubmitEditing = {() => console.log(this.state.term)}
/>

或者您也可以调用“ onFormSubmit”函数