J'ai un petit composant et j'essaie de déclencher une fonction à l'improviste. J'ai d'abord essayé ce qui suit: Onpress = {this.slider} et je n'ai pas reçu de journal de console, puis j'ai essayé tinp = {() => {this.slider}} et n'a toujours pas reçu de journal de console. Enfin j'ai essayé oigning = {console.log ("bonjour")} et reçu un journal de console. Donc, je n'appelle clairement pas la fonction du curseur correctement. Je ne suis pas sûr de ce que je fais mal - quelque chose de stupide, je suis sûr. Toute aide est appréciée.
class newWave extends Component {
state = {
format: "TTS"
}
slider = () => {
console.log("hi");
if (this.state.format === "TTS")
this.setState({ format: "Voice" })
else if (this.state.format === "Voice")
this.setState({ format: "TTS" })
}
render() {
return (
<View style={{height:"100%", width: "100%", flexDirection:"column", justifyContent: "center", alignItems:"center"}}>
<View style={{height: "75%", width:"75%", backgroundColor:"blue", borderRadius:20, flexDirection:"row", justifyContent:"flex-end"}}>
<View style ={{backgroundColor:"transparent", height: "10%", width:"30%", margin: "2%"}}>
<View style = {{backgroundColor:"orange", height: "100%", width:"100%", borderRadius: 20, flexDirection:"row", justifyContent:this.state.format==="TTS"?"flex-start":"flex-end", alignItems:"center"}}>
<View style = {{backgroundColor:"green", height: "98%", width:"75%", borderRadius: 20, marginLeft: "1%", marginRight: "1%"}} onPress={()=>{this.slider}}></View>
</View>
</View>
</View>
</View>
);
}
}
export default newWave;
6 Réponses :
Permettez-moi de vous présenter à Touchabilité. La vue ne fournit pas un accessoire irréprochable.
<TouchableOpacity onPress={()=> {
this.slider();
}
}>
<View style = {{backgroundColor:"green", height: "98%", width:"75%", borderRadius: 20, marginLeft: "1%", marginRight: "1%"}} ></View>
</TouchableOpacity>
À propos de la fois que vous avez vu la console.log, olfress = {console.log ("hello")} code> est parce que la mise en place des supports "()" déclenchera la fonction où qu'ils soient, l'enregistrant pendant chaque rendu. Si vous avez conservé le code et avez essayé de faire pression sur l'ONPRESS, vous remarqueriez qu'il ne s'agira pas réellement
Dans Reagel Native, il existe trois façons d'appeler On TressApple: TouchAbaNativeFeedBack, TouchableHighlight et TouchablePacity P>
Alors enveloppez la vue à l'intérieur de l'un de ces trois, ce trois a des accessoires à l'impression. P>
Utilisez En outre, il y a Deux façons de lier les rappels dans réact. P > TouchableOpacity Code> '' S href = "https://facebook.github.io/react-native/docs/touchablewithoutfeedback#onpress" rel = "nfollow NOREFERRER"> Onpress () Code> Prop.
function slider() {}
<TouchableOpacity onPress={() => this.slider()} />
...
</TouchableOpacity>
S'il vous plaît essayez ceci. Cela devrait fonctionner. La vue ne vient pas avec l'événement OnPress
<Button
title='Hello'
style = {{backgroundColor:"green", height: "98%", width:"75%", borderRadius: 20, marginLeft: "1%", marginRight: "1%"}} onPress={()=>{this.slider()}}>
</Button>
Essayez ce code:
<TouchableOpacity
style={{
backgroundColor: 'green',
height: '98%',
width: '75%',
borderRadius: 20,
marginLeft: '1%',
marginRight: '1%',
}}
onPress={this.slider}
/>
Vous devez envelopper la vue où vous souhaitez cliquer avec un olfress = {} code> sur le touchableopacity, comme suit <TouchableOpacity onPress={CALL_YOUR_FUNCTION_HERE}>
<View .... /> //add your view here
</TouchableOpacity>
Il suffit de changer
afficher code> composant dansbouton code> outouchablePacacité code>