배열 개체의 목록을 필터링 한 다음 새 DataSource로 ListView에 표시하려고합니다. 그러나 목록은 필터링되지 않습니다. 내 필터 기능이 올바르게 작동하는 것 같습니다. (console.log에서 확인)ListView에서 데이터를 필터링하는 방법 React-native?
내 상태를 소품으로 매핑하는 데 Redux를 사용하고 있습니다. 그리고 나서 소품을 걸러 내려고합니다. 이것이 잘못된 방법입니까? 여기
내 코드입니다 :/*global fetch:false*/
import _ from 'lodash';
import React, { Component } from 'react';
import { ListView, Text as NText } from 'react-native';
import { connect } from 'react-redux';
import { Actions } from 'react-native-router-flux';
import {
Container, Header, Item,
Icon, Input, ListItem, Text,
Left, Right, Body, Button
} from 'native-base';
import Spinner from '../common/Spinner';
import HealthImage from '../misc/HealthImage';
import { assetsFetch } from '../../actions';
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
class AssetsList extends Component {
componentWillMount() {
this.props.assetsFetch();
// Implementing the datasource for the list View
this.createDataSource(this.props.assets);
}
componentWillReceiveProps(nextProps) {
// Next props is the next set of props that this component will be rendered with.
// this.props is still equal to the old set of props.
this.createDataSource(nextProps.assets);
}
onSearchChange(text) {
const filteredAssets = this.props.assets.filter(
(asset) => {
return asset.name.indexOf(text) !== -1;
}
);
this.dataSource = ds.cloneWithRows(_.values(filteredAssets));
}
createDataSource(assets) {
this.dataSource = ds.cloneWithRows(assets);
}
renderRow(asset) {
return (
<ListItem thumbnail>
<Left>
<HealthImage health={asset.health} />
</Left>
<Body>
<Text>{asset.name}</Text>
<NText style={styles.nText}>
Location: {asset.location} |
Serial: {asset.serial_number}
</NText>
<NText>
Total Samples: {asset.total_samples}
</NText>
</Body>
<Right>
<Button transparent onPress={() => Actions.assetShow()}>
<Text>View</Text>
</Button>
</Right>
</ListItem>
);
}
render() {
return (
<Input
placeholder="Search"
onChangeText={this.onSearchChange.bind(this)}
/>
<ListView
enableEmptySections
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
);
}
}
const mapStateToProps = state => {
return {
assets: _.values(state.assets.asset),
spinner: state.assets.asset_spinner
};
};
export default connect(mapStateToProps, { assetsFetch })(AssetsList);
은 내가 잘못 여기서 뭐하는 거지?
그것은 작동합니다! 정말 고맙습니다!! 내가해야만하는 것은 텍스트의 생성자에서 초기 상태를 설정하는 것이 었습니다. 감사합니다. Tim! –
아, 맞아. 그렇지 않으면 상태 자체가 정의되지 않습니다. 다행입니다. –
답변을 업데이트했습니다. 생성자 대신 ES7 속성 설정자를 사용했습니다. –