2017-04-19 20 views
0

私は非常に単純な問題で立ち往生しています。私はusdの入力とcnyの入力を持つフォームを持っています。私はそれらのうちの1つを入力したいと思います。もう1つは、caculationによって値を表示します。Reactネイティブのテキスト入力値の問題


enter image description here

import React, { Component } from 'react'; 
    import { View, Text, TextInput } from 'react-native'; 
    class Buy extends Component { 
     constructor() { 
      super(); 
      this.state = { 
       usd: '', 
       cny: '' 
      }; 
     } 
     render() { 
     return (
     <View style={styles.inputSection}> 
      <View style={styles.leftInputSection}> 
       <Text style={styles.inputLabel}>USD</Text> 
       <TextInput 
        placeholder='0.0' 
        placeholderTextColor='#999999' 
        style={styles.inputStyle} 
        keyboardType={'numeric'} 
        onChangeText={(usd) => this.setState({ usd })} 
        value={((this.state.cny) * 7).toString()} 
       /> 
      </View> 
      <View style={styles.rightInputSection}> 
       <Text style={styles.inputLabel}>CNY</Text> 
       <TextInput 
        placeholder='0.0' 
        placeholderTextColor='#999999' 
        style={styles.inputStyle} 
        keyboardType={'numeric'} 
        onChangeText={(cny) => this.setState({ cny })} 
        value={((this.state.usd)/7).toString()} 
       /> 
      </View> 
     </View> 
    ); 
    } 
} 

答えて

-1

私はTextInputinput要素を交換しても、他のプロパティを指定し、この例を確認し、これはあなたが欲しいものだと思う:

class App extends React.Component{ 
 

 
    constructor(){ 
 
     super(); 
 
     this.state = {a: '', b: ''} 
 
    } 
 
    
 
    render(){ 
 
     return(
 
     <div> 
 
      <input 
 
      value={this.state.a} 
 
      onChange={(e)=>this.setState({b: e.target.value *7, a: e.target.value})} 
 
      /> 
 
      <input 
 
      value={this.state.b} 
 
      onChange={(e)=>this.setState({a: e.target.value/7, b: e.target.value})} 
 
      /> 
 
     </div> 
 
    ) 
 
    } 
 

 
} 
 

 
ReactDOM.render(<App/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 

 
<div id='app'/>

:それはあなたが他にtextInputを使うべきではありません反応し、ネイティブonChangeの内側に何react-native onChange提供等によりをe.target.value を交換するようにあなたは、変更を行う必要があり

+0

あなたの答えは、ネイティブ反応し、反応しないためです。 * e *パラメータを指定していないネイティブのtextInput "onChange"に反応して、入力の値を提供するだけです。 –

+0

@MeysamIzadmehr stackoverflow someonesコードを書くためのものではなく、あなたの問題のアイデアや解決策を与える場所です。私の答えでは、 'input'要素を置き換えて変更を行い、' e'を何反応ネイティブの 'onChange'は大きなものではなく、私は彼にウェブのための実用的なソリューションを与えました。反応ネイティブの非常に基本的な知識を持つ誰でも簡単に変換できます。 Btwネイティブで直接的に優れたソリューションを提供できると思う場合は、新しい回答を追加することもできます:) –

+0

反応ネイティブのtextInput onChangeで* e.target.value *を使用することはできません。 –

0

でコードを使用するように、ウェブのためのソリューションです状態を値として返します。 @Mayankスニペットとして、SETSTATE 2にtextInputのonChangeTextで CNY USD &

 <TextInput 
     placeholder='0.0' 
     placeholderTextColor='#999999' 
     style={styles.inputStyle} 
     keyboardType={'numeric'} 
     onChangeText={(usd) => this.setState({cny: usd * 7, usd: usd})} 
     value={(this.state.usd).toString()} 
    /> 
    <TextInput 
     placeholder='0.0' 
     placeholderTextColor='#999999' 
     style={styles.inputStyle} 
     keyboardType={'numeric'} 
     onChangeText={(cny) => this.setState({cny: cny, usd: cny/7})} 
     value={(this.state.cny).toString()} 
    /> 
関連する問題