gpt4 book ai didi

reactjs - 如何在react-native中获取文本组件onPress中的文本

转载 作者:行者123 更新时间:2023-12-03 13:36:48 27 4
gpt4 key购买 nike

我想获取“文本”组件“onPress”中的文本并将其设置为组件的状态变量。这是我的代码 -

var MyComponent = React.createClass({
getInitialState: function(){
return {
myStateVariable: 'New York'
}
},
render: function(){
return (
<Text onPress={()=>{
this.setState({
myStateVariable: 'London' // This works fine
myStateVariable: this.children // This is not working
});
}
}>
London
</Text>
<Text>{this.state.myStateVariable}</Text>
);
}
});

如何动态获取“Text”组件中的文本?

最佳答案

根据您对我的评论的反馈,您应该尝试像这样编写 MyComponent -

var MyComponent = React.createClass({
onPress:function(i){
let city = this.props.cities[i];
// do something with city.
},
renderCities:function(){
this.props.cities.map((city, i)=>{
return <Text key={i} onPress={this.onPress.bind(this, i)}>{city}</Text>;
});
},
render: function(){
return (
<div>
{this.renderCities()}
</div>
);
}
});

关于reactjs - 如何在react-native中获取文本组件onPress中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com