我有一个有效的ASK组件,它基本上需要用户输入并推送到Firebase数据库。
import React from 'react';
import {
Image,
Linking,
Platform,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
ListView,
TextInput,
} from 'react-native';
import Input from '../components/Input';
import {firebaseApp} from '../Firebase';
export default class Ask extends React.Component {
constructor(props) {
super(props);
this.itemsRef = firebaseApp.database().ref();
this.state = {
text:''
};
}
additem(){
this.itemsRef.push({ title: this.state.text })
this.setState({text:''})
}
render() {
return (
<View style={styles.container}>
<TextInput style={styles.textinput}
placeholder="Insert Item Here!"
onChangeText={(text) => this.setState({text})}
onSubmitEditing= {this.additem.bind(this)}
value={this.state.text}
>
</TextInput>
{/* Many other components here */}
</View>
);
}
}
我想将TextInput组件移动到一个单独的文件(创建一个INPUT组件)。 (使INPUT组件成为表示组件,ASK组件成为容器组件)
但是,在Ask Component中,我不知道如何检索输入组件的text
状态值,以便我可以调用this.itemsRef.push({ title: THE_TEXT_STATE_VALUE_OF_INPUT_COMPONENT })
这是我的代码。
Input.js
import React, { Component } from 'react'
import { View, Text, StyleSheet,TextInput,PropTypes} from 'react-native'
export default class Input extends React.Component {
constructor(props) {
super(props);
this.state = {text:''}
}
render() {
return (
<TextInput style={styles.textinput}
placeholder = {this.props.placeholder}
onChangeText={(text) => this.setState({text})}
onSubmitEditing= {this.props.AddItem}
>
</TextInput>
)
}
}
Ask.js
import React from 'react';
import {
Image,
Linking,
Platform,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
ListView,
TextInput,
} from 'react-native';
import Input from '../components/Input';
import {firebaseApp} from '../Firebase';
export default class Ask extends React.Component {
constructor(props) {
super(props);
this.itemsRef = firebaseApp.database().ref();
this.state = {
text:''
};
}
additem(){
this.itemsRef.push({ title: this.state.text })
this.setState({text:''})
}
render() {
return (
<View style={styles.container}>
<Input
placeholder="Inser here" AddItem={this.additem.bind(this)}> ////// THIS IS WRONG
</Input>
{/* Many other components here */}
</View>
);
}
}
答案 0 :(得分:0)
更新您的Ask.js,如下所示。
import React from 'react';
import {
Image,
Linking,
Platform,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
ListView,
TextInput,
} from 'react-native';
import Input from '../components/Input';
import {firebaseApp} from '../Firebase';
export default class Ask extends React.Component {
constructor(props) {
super(props);
this.itemsRef = firebaseApp.database().ref();
this.state = {
text:''
};
this.inputChange = this.inputChange.bind(this);
this.additem = this.additem.bind(this);
}
additem(){
this.itemsRef.push({ title: this.state.text })
this.setState({text:''})
}
inputChange(entered_text){
this.setState({text: entered_text })
}
render() {
return (
<View style={styles.container}>
<Input
placeholder="Inser here" AddItem={ this.additem } inputChange={ this.inputChange }> ////// THIS IS WRONG
</Input>
{/* Many other components here */}
</View>
);
}
}
和
Input.js
import React, { Component } from 'react'
import { View, Text, StyleSheet,TextInput,PropTypes} from 'react-native'
export default class Input extends React.Component {
constructor(props) {
super(props);
this.state = {text:''}
}
render() {
return (
<TextInput style={styles.textinput}
placeholder = {this.props.placeholder}
onChangeText={ this.props.inputChange }
onSubmitEditing= {this.props.AddItem}
>
</TextInput>
)
}
}
现在输入组件将更新Ask组件的状态。