【问题标题】:Change Icon activeIndex via React Native通过 React Native 更改图标 activeIndex
【发布时间】:2018-07-04 09:55:31
【问题描述】:

如何通过 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>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs react-native tabs native-base


    【解决方案1】:

    您在编写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
      });
    }
    

    【讨论】:

    • 我改变了,什么也没发生,和过去一样,0 是活动的。
    • @SedricHeidarizarei 我发现了第二个错误。我更新了答案。
    • 这很奇怪,因为我的 segmentClicked 不起作用,你的 segmentClicked 起作用。这是我点击的片段:``` // segmentClicked = (index) => { // this.setState = ({ // activeIndex: index // }) // }```
    • @SedricHeidarizarei 是的,如果你仔细观察你会发现你正在写setState =。您不应该覆盖setState,而是将其作为函数调用setState()
    • 啊,是的,谢谢。欣赏它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    相关资源
    最近更新 更多