0

我有一个可用的 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 中,我不知道如何检索textInput Component 的状态值,以便我可以调用this.itemsRef.push({ title: THE_TEXT_STATE_VALUE_OF_INPUT_COMPONENT })

这是我的代码。

输入.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>
    );
  }
}
4

1 回答 1

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>
    );
  }
}

输入.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 组件的状态。

于 2017-04-13T04:38:08.923 回答