切换值不在renderRow中更新

时间:2016-10-18 09:50:55

标签: javascript react-native

我的状态中有一个键值数组,其中包含指示我的开关值的布尔值。

当我触发Switch组件时,在我的状态下正确更改了值,但我的Switch的值保持不变。就像组件没有更新一样。我在另一个项目中做了完全相同的事情,它正在那里工作。

_changeValue(k) {
    switchesValues[k] = !switchesValues[k]
    this.setState({
      switches: switchesValues
    })
}

_renderEventRow(allergiesList) {
    var k = allergiesList.key

    return (
      <View style={{flexDirection: 'row', height: 50, alignItems: 'center', paddingLeft: 10, borderBottomWidth: 0.5, borderColor: 'lightgray'}}>
        <Text style={{flex: 1, color: '#5f5f5f'}}>{allergiesList.name}</Text>
        <Switch style={{marginRight: 10}} value={this.state.switches[k]} onValueChange={() => this._changeValue(k)}/>
      </View>
    )
}

我的列表视图:

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2})

constructor(props) {
super(props)
    this.state = {
      ds:[],
      dataSource:ds,
      switches: []
    }
}

componentDidMount() {
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(this.state.ds),
    })
    this.findAllergies()
}

render() {
    return(
      <ListView
        dataSource={this.state.dataSource}
        renderRow={(rowData) => { return this._renderEventRow(rowData)}}
      />)}
    )
}

findAllergies() {
  var that = this
  firebase.database().ref("allergies").on("value", (snap) => {
    var items = [];

    snap.forEach((child) => {
      items.push({
        name: child.val(),
        key: child.key,
      })
    })

    that.setState({
      dataSource: that.state.dataSource.cloneWithRows(items),
    });
  })
}

1 个答案:

答案 0 :(得分:0)

您好我已经创建了一个示例应用来实现您的要求。看看下面的代码。

import React, {Component} from 'react';
import {
    AppRegistry,
    Text,
    View,
    TouchableOpacity,
    Switch,
    ListView,
} from 'react-native';

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2});

export default class AwesomeProject extends Component {

    constructor(props) {
        super(props)
        this.state = {
            ds: [],
            dataSource: ds,
            switches: []
        }
    }

    componentDidMount() {
        let allergies = [];
        this.switchesValues = [];
        for (let i = 0; i <= 5; i++) {
            allergies.push({
                key: i,
                name: 'Name ' + i,
            });
            this.switchesValues[i] = false;
        }
        this.setState({
            dataSource: this.state.dataSource.cloneWithRows(allergies),
            switches: this.switchesValues,
        })
    }

    render() {
        return (
            <ListView
                dataSource={this.state.dataSource}
                renderRow={(rowData) => { return this._renderEventRow(rowData)}}
            />);
    }

    _changeValue(k) {
        console.log('Status '+JSON.stringify(this.switchesValues))
        this.switchesValues[k] = !this.switchesValues[k];
        this.setState({
            switches: this.switchesValues,
        })
    }

    _renderEventRow(allergiesList) {
        var k = allergiesList.key

        return (
            <View
                style={{flexDirection: 'row', height: 50, alignItems: 'center', paddingLeft: 10, borderBottomWidth: 0.5, borderColor: 'lightgray'}}>
                <Text style={{flex: 1, color: '#5f5f5f'}}>{allergiesList.name}</Text>
                <Switch style={{marginRight: 10}} value={this.state.switches[k]}
                        onValueChange={() => this._changeValue(k)}/>
            </View>
        )
    }
}

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);