gpt4 book ai didi

javascript - 通过 React Native 更改图标 activeIndex

转载 作者:行者123 更新时间:2023-11-29 23:19:41 25 4
gpt4 key购买 nike

如何通过 React Native 更改图标 activeIndex?我正在使用 native-base 模块,但不起作用,只是 activeIndex == 0 处于事件状态,我的功能不起作用。

代码:

import {Icon, Button} from 'native-base';
type Props = {};
export default class App extends Component<Props> {
constructor(props) {

super(props);
this.segmentClicked = this.segmentClicked.bind(this);
this.state = {
activeIndex: 0
}
}

segmentClicked = (index) => {
this.setState = ({
activeIndex: index
})
}
render() {
return (
<View style={styles.container}>
<View style={{flexDirection: 'row', justifyContent: 'space-around', borderTopWidth: 1, borderTopColor: '#eae5e5'}}>
<Button
onPress={this.segmentClicked(0)}
active={this.state.activeIndex == 0}
>
<Icon name='ios-apps-outline'
style={[this.state.activeIndex == 0 ? {} : {color: 'gray'}]}
/>
</Button>
<Button
onPress={this.segmentClicked(1)}
active={this.state.activeIndex == 1}
>
<Icon name='ios-list-outline'
style={[this.state.activeIndex == 1 ? {} : {color: 'gray'}]}
/>
</Button>
<Button
onPress={this.segmentClicked(2)}
active={this.state.activeIndex == 2}
>
<Icon name='ios-people-outline'
style={[this.state.activeIndex == 2 ? {} : {color: 'gray'}]}
/>
</Button>
<Button
onPress={this.segmentClicked(3)}
active={this.state.activeIndex == 3}
>
<Icon name='ios-bookmark-outline'
style={[this.state.activeIndex == 3 ? {} : {color: 'gray'}]}
/>
</Button>
</View>
</View>
);
}
}

最佳答案

当您编写 this.segmentClicked(1) 时,您正在调用该函数。相反,您想提供一个函数,当按下 Button 时, 调用该函数。

例如,您可以创建一个新的内联箭头函数。

<Button
onPress={() => this.segmentClicked(1)}
active={this.state.activeIndex == 1}
>
<Icon name='ios-list-outline'
style={[this.state.activeIndex == 1 ? {} : {color: 'gray'}]}
/>
</Button>

您还必须调用 setState 函数,而不是为其分配新值。

segmentClicked = (index) => {
this.setState({
activeIndex: index
});
}

关于javascript - 通过 React Native 更改图标 activeIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51170864/

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