Uygulamamdaki bir düğmenin stiline basıldığında değişiklik yapılması istiyorum. Bunu yapmanın en iyi yolu nedir? BöyleDüğme stili üzerinde değişiklik yap React Native
cevap
kullanın TouchableHighlight
. İşte bir örnek
:
'use strict';
import React, {
Component,
StyleSheet,
PropTypes,
View,
Text,
TouchableHighlight,
} from 'react-native';
export default class Home extends Component {
constructor(props) {
super(props);
this.state = { pressStatus: false };
}
_onHideUnderlay(){
this.setState({ pressStatus: false });
}
_onShowUnderlay(){
this.setState({ pressStatus: true });
}
render(){
return (
<View style={styles.container}>
<TouchableHighlight
activeOpacity={1}
style={ this.state.pressStatus ? styles.buttonPress : styles.button }
onHideUnderlay={this._onHideUnderlay.bind(this)}
onShowUnderlay={this._onShowUnderlay.bind(this)}
>
<Text style={ this.state.pressStatus ? styles.welcomePress : styles.welcome }>{this.props.text}</Text>
</TouchableHighlight>
</View>
)
}
}
Home.propTypes = {
text: PropTypes.string.isRequired,
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
color: '#000066'
},
welcomePress: {
fontSize: 20,
textAlign: 'center',
margin: 10,
color: '#ffffff'
},
button: {
borderColor: '#000066',
borderWidth: 1,
borderRadius: 10,
},
buttonPress: {
borderColor: '#000066',
backgroundColor: '#000066',
borderWidth: 1,
borderRadius: 10,
},
});
Bu gerçekten yararlı! Ve 'onHideUnderlay' &' onShowUnderlay' yöntemleri 'onPress' ile çakışma değildir. Bu biraz kafamı karıştırdı. –
Sadece '
'onHideUnderlay' &' onShowUnderlay' hakkında bir şey bilmiyordum! Böyle bir şeyi nasıl başaracağınızı öğrenmek için sonsuza kadar googling oldu. Teşekkür ederim! – gregblass
kullanım şey: Gerçekten olursa olsun nasıl uygularım değildir
render() {
var _class = "button";
var _class.concat(this.state.onClicked ? "-pressed" : "-normal") ;
return (
<div>
<button
onClick={this.handlerButtonOnClick}
className={_class}>Press me !</button>
</div>
);
}
: Harici CSS kullanırsanız
class A extends React.Component {
constructor(props){
super(props);
this.state = {
onClicked: false
}
this.handlerButtonOnClick = this.handlerButtonOnClick.bind(this);
}
handlerButtonOnClick(){
this.setState({
onClicked: true
});
}
render() {
var _style;
if (this.state.onClicked){ // clicked button style
_style = {
color: "red"
}
}
else{ // default button style
_style = {
color: "blue"
}
}
return (
<div>
<button
onClick={this.handlerButtonOnClick}
style={_style}>Press me !</button>
</div>
);
}
}
, stil özelliğinin yerine SinifAdi kullanabilirsiniz senin CSS. Gözlerinizi "handlerButtonOnClick" yönteminde tutun.
Durum değiştiğinde, bileşen yeniden oluşturulur ("render" yöntemi yeniden çağrılır).
İyi şanslar;)
tepki ana makinesinde bir düğme bileşeni yok. Diğerleri arasında touchableHighlight, touchableNativeFeedback kullanır. – Nico
problem, tepki-native ile ilgili değil ... problem, bileşen üzerindeki stilleri nasıl ve ne zaman uygulayacağını anlamaktır. Akış her zaman aynıdır: handleEvent -> handler -> setState -> render. Cevabım bu akışla ilgili, tepki veren öğeler hakkında değil. –
Bu ES6 içinde Besart Hoxhaj 'ın cevaptır. Buna cevap verdiğimde, React Native 0.34'tür.
underlayColor
<TouchableHighlight style={styles.btn} underlayColor={'gray'} />
https://facebook.github.io/react-native/docs/touchablehighlight.html
pervane kullanın
? Veya dokunduğunda onu değiştirmeli ve dokunma durduktan sonra değişen durumu mu tutmalısınız?'underlayColor ', hafif bir opaklıkla görüntülenecektir. 'ActiveOpacity' öğesini 0 olarak ayarlamak, basılmamış duruma uygulanan stilleri tamamen yok sayar. –
- 1. React Native
- 2. React Native
- 3. React Native
- 4. React Native
- 5. React Native
- 6. React native paketini bulamadı
- 7. ViewPagerAndroid, React-Native
- 8. Değişiklik ListPreference stili
- 9. React-Native + kripto: Nasıl React-Native içinde HMAC üretilir?
- 10. Stilleri React-Native
- 11. React Native Infinite Kaydırma
- 12. React Native View Render
- 13. React Native: Bileşenin,
- 14. React Native: 0.41 app.json
- 15. React-Native Gezinme Çekmecesi
- 16. React Native File seçici
- 17. Dismiss Keyboard React-Native
- 18. React-Native projesi
- 19. React web ve React-Native projelerini birleştirme?
- 20. ToolBar Düğme Stili
- 21. React Native: Çok satırlı textinput'ı klavye üzerinde görünür durumda tutma
- 22. Bir iOS Today Widget eklentisi React Native üzerinde çalışıyor
- 23. Windows gractiv.properties dosyası üzerinde APK dosyası oluşturuluyor React Native
- 24. Düğme stili HTML, CSS, Jquery
- 25. React Native: Android Yerel Modülü
- 26. React Native modüllerindeki Bağımlılık Enjeksiyonu
- 27. React Native ListView scrollToEnd çalışmıyor
- 28. React-native: FlatList öğelerini sarmak
- 29. setNativeProps ({adım: num}), React Native
- 30. React Native Yinelenen modül adı
kullanıcı bastırarak değilken sadece basılı edilirken değiştirmek ve geri değiştirmeye ihtiyacınız var mı:
–Kullanıcıya basmadığı zaman geri dönmek için. – domi91c