【问题标题】:Can't getting correct state value无法获得正确的状态值
【发布时间】:2020-02-23 20:37:08
【问题描述】:

我是 react-native 的新手。我尝试创建一组类似于单选按钮的按钮。
ButtonsGroup 显示正确,但我从 App.js 得到不正确的 selectedIndex。当我选择第二个按钮时,我得到了 selectionIndex 0 而不是 1。

ButtonsGroup .js

import React, {Component} from 'react';
import {View, Text, TouchableNativeFeedback} from 'react-native';

export default class ButtonsGroup extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedIndex: this.props.selectedIndex,
      buttons: this.props.buttons,
    };
  }

  _updateIndex = index => {
    this.setState({selectedIndex: index});
    this.props.onPress(this.state.selectedIndex);
  };

  _renderButtons() {
    const buttons = this.state.buttons.map((title, index) => {
      return (
        <View
          key={index}
          style={{
            height: this.props.height,
            backgroundColor: 'transparent',
            flex: 1,
            borderRadius: 18,
            elevation: 2,
            marginRight: index < this.state.buttons.length - 1 ? 10 : 0,
          }}>
          <TouchableNativeFeedback
            background={TouchableNativeFeedback.Ripple('dark', true)}
            onPress={() => this._updateIndex(index)}>
            <View
              style={{
                flex: 1,
                alignItems: 'center',
                justifyContent: 'center',
                backgroundColor:
                  this.state.selectedIndex == index ? '#2566d4' : 'white',
              }}>
              <Text
                style={{
                  color: this.state.selectedIndex == index ? 'white' : 'black',
                }}>
                {title}
              </Text>
            </View>
          </TouchableNativeFeedback>
        </View>
      );
    });

    return buttons;
  }

  render() {
    console.log(this.props);
    return (
      <View style={{...this.props.style, flexDirection: 'row'}}>
        {this._renderButtons()}
      </View>
    );
  }
}

App.js

import React, {Component} from 'react';
import {View} from 'react-native';
import ButtonsGroup from './components/ButtonsGroup';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedIndex: 0,
    };
    this._updateIndex = this._updateIndex.bind(this);
  }

  _updateIndex(selectedIndex) {
    this.setState({selectedIndex:selectedIndex});
  }

  render() {
    const {selectedIndex} = this.state;
    return (
        <View>
          <ButtonsGroup
            onPress={this._updateIndex}
            buttons={['button1', 'button2']}
            selectedIndex={selectedIndex}
            height={38}
            style={{margin: 10}}
          />
        </View>
    );
  }
}

不要提供来自 'react-native-elements' 的 GroupButton。

【问题讨论】:

    标签: react-native button components state


    【解决方案1】:

    你正在立即使用 this.state.selectedIndex

    _updateIndex = index => {
      this.setState({selectedIndex: index});
      this.props.onPress(this.state.selectedIndex);
    };
    

    你应该使用

    _updateIndex = index => {
        this.setState({selectedIndex: index});
        this.props.onPress(index);
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多