Javascript 单击react native中的按钮时更改状态
我对react native很陌生,只想让一个函数更改单击按钮的状态,而不是其他具有相同功能的函数 正如我在标题中解释的,这里是一个示例代码 请任何帮助&我知道这可能是一个很有说服力的问题,但任何回答都会有帮助 非常感谢Javascript 单击react native中的按钮时更改状态,javascript,reactjs,react-native,expo,Javascript,Reactjs,React Native,Expo,我对react native很陌生,只想让一个函数更改单击按钮的状态,而不是其他具有相同功能的函数 正如我在标题中解释的,这里是一个示例代码 请任何帮助&我知道这可能是一个很有说服力的问题,但任何回答都会有帮助 非常感谢 export default class App extends Component { constructor(){ super(); this.state = { opened: true, } } componentHide
export default class App extends Component {
constructor(){
super();
this.state = {
opened: true,
}
}
componentHideAndShow = () =>{
this.setState(previousState => ({opened: !previousState.opened}))
}
render() {
return (
{
this.state.opened ? <Text> hello</Text> : <Text> hello sdfsdfsdf</Text>
}
<Text onPress={this.componentHideAndShow}>test</Text>
{
this.state.opened ? <Text> hello</Text> : <Text> hello sdfsdfsdf</Text>
}
<Text onPress={this.componentHideAndShow}>test</Text>
);
}
}
导出默认类应用程序扩展组件{
构造函数(){
超级();
此.state={
是的,
}
}
componentHideAndShow=()=>{
this.setState(previousState=>({opened:!previousState.opened}))
}
render(){
返回(
{
this.state.opened?您好:您好sdfsdfsdf
}
测试
{
this.state.opened?您好:您好sdfsdfsdf
}
测试
);
}
}
这应该行得通
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
opened: [true, true]
};
}
componentHideAndShow = index => {
const opened = this.state.opened;
opened[index] = !opened[index];
this.setState({ opened: opened });
};
render() {
return (
<View>
{this.state.opened[0] ? (
<Text> hello</Text>
) : (
<Text> hello sdfsdfsdf</Text>
)}
<Button onPress={() => this.componentHideAndShow(0)}>test</Button>
{this.state.opened[1] ? (
<Text> hello</Text>
) : (
<Text> hello sdfsdfsdf</Text>
)}
<Button onPress={() => this.componentHideAndShow(1)}>test</Button>
</View>
);
}
}
import React,{Component}来自'React';
从“react native”导入{视图、文本、按钮};
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
打开:[真的,真的]
};
}
componentHideAndShow=索引=>{
const opened=this.state.opened;
已打开[索引]=!已打开[索引];
this.setState({opened:opened});
};
render(){
返回(
{this.state.opened[0](
你好
) : (
你好,SDFSDFDF
)}
this.componentHideAndShow(0)}>test
{this.state.opened[1](
你好
) : (
你好,SDFSDFDF
)}
这个.componentHideAndShow(1)}>test
);
}
}
编辑:如果您不知道项目数,可以这样做:
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
const myArrayOfStrings = ['hello1', 'hello2', 'hello3', 'hello4', 'hello5'];
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
opened: undefined
};
}
componentDidMount() {
let opened = [];
myArrayOfStrings.map(item => {
opened.push(true);
});
this.setState({ opened: opened });
}
componentHideAndShow = index => {
const opened = this.state.opened;
opened[index] = !opened[index];
this.setState({ opened: opened });
};
render() {
const output = myArrayOfStrings.map((item, index) => {
return (
<View>
<Text>
{this.state.opened[index]
? `${item} is opened`
: `${item} is opened`}
</Text>
<Button onPress={() => this.componentHideAndShow(0)}>test</Button>
</View>
);
});
return <View>{output}</View>;
}
}
import React,{Component}来自'React';
从“react native”导入{视图、文本、按钮};
常量myArrayOfStrings=['hello1','hello2','hello3','hello4','hello5'];
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
打开:未定义
};
}
componentDidMount(){
让开放=[];
myArrayOfStrings.map(项=>{
打开。推送(true);
});
this.setState({opened:opened});
}
componentHideAndShow=索引=>{
const opened=this.state.opened;
已打开[索引]=!已打开[索引];
this.setState({opened:opened});
};
render(){
const output=myArrayOfStrings.map((项目,索引)=>{
返回(
{this.state.opened[索引]
?`${item}已打开`
:`${item}已打开`}
this.componentHideAndShow(0)}>test
);
});
返回{output};
}
}
感谢您的回复,它非常适用于本案例,但如果我有未知数量的按钮,其内容来自api,例如,我如何处理本案例,实际上这是我正在处理的案例,感谢againI编辑了答案,它将适用于未知数量的按钮。如果可行的话,别忘了接受这个答案。不幸的是,它不起作用,如果你有任何想法,请告诉我,谢谢。通常情况下,数组不会像这里那样硬编码。它将来自数据库、道具或某种计算。因此,您必须更新componentDidMount()的状态。检查我的最新答案。