2015-11-26 86 views

回答

0

做裁判使用的,可以作爲參考作用的成分組件

例如:

<View> 
<TextInput style={styles.textInput} 
      ref={'usrname'} 
      placeholder={'Username'} 
      placeholderTextColor={'red'} 
      onChangeText={(userName) => this.setState({userName})} 
      value={this.state.userName} /> 
<TouchableOpacity onPress={this._onPressButton}> 
       <View style={styles.buttonContainer}> 
       <Text> Submit </Text> 
       </View> 
      </TouchableOpacity> 
</View> 

和onPress:

_onPressButton: function(e){ 
    var x = this.state.userName; 
    alert(x); //we can also use alert(this.state.userName) 
    }, 
+0

謝謝... @Pramod Mg但我怎麼能在我的java代碼中使用它?我可以將它傳遞給我的模塊嗎?像Mymodule._module.func(x); – JA21

+0

y到javacode>? –

+1

原因我想在我的java中使用我的CustomView .. 查看canvasView = findViewById(R.id.CustomView); 洙,我可以捕捉到我的CustomView裏面的東西,問題是react-native沒有xml,我知道它非常容易,如果它有一個xml文件,我可以說.. findViewById()... – JA21

1

這可以通過React.findNodeHandle完成。

在你的情況,

... 

var { findNodeHandle } = React; 

... 

render: function() { 
    return (
    <View> 
     <CustomView ref="customView"> 
     ... 
     </CustomView> 
    </View> 
); 
}, 

somethingLikeComponentDidMount: function() { 
    var customViewNativeID = findNodeHandle(this.refs.customView); 
    // Something else... 
} 

... 

可能做的工作。

有關工作示例(本機綁定兩個組件),請檢出here作爲JS部分,here作爲本機Java部分。