我有這樣的代碼 它的功能是搜索和過濾器的渲染,數據來自this.props.allList,但它不工作,我認爲錯誤是在狀態,但我不知道的setState不是一成不變的
// component.jsx 'use strict';
import React, {Component} from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import Immutable from 'immutable';
export default class ListComponent extends Component {
constructor(props) {
super(props);
this.state = {
allList: Immutable.List(),
filteredData: Immutable.List(),
};
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
componentWillMount() {
this.setState({
allList: Immutable.fromJS(this.props.allList).toList(),
filteredData: Immutable.fromJS(this.props.allList).toList()
});
}
filterData(event) {
event.preventDefault();
const regex = new RegExp(event.target.value, 'i');
const filtered = this.state.allList.filter(function(datum) {
return (datum.get('name').search(regex) > -1);
});
this.setState({
filteredData: filtered,
});
}
render() {
const { filteredData } = this.state;
const prettyRows = filteredData.map(function(datum) {
return (
<tr>
<td>{ datum.get("status") }{datum.status}</td>
<td>{ datum.get("count") }{datum.count}</td>
</tr>
);
});
return(
<div>
<input
type="text"
className="form-control"
onChange={ this.filterData.bind(this) }
placeholder="Search" />
<table
<thead>
<tr>
<th>id </th>
<th>Name</th>
</tr>
</thead>
<tbody>
{ prettyRows }
</tbody>
</table>
</div>);
}
}
的JSON
{
"count": 3,
"status": "SUCCESS",
"tours": [
{
"title": "item 1",
},
{
"title": "item 2",
},
{
"title": "item 3",
},
]
}
這裏codepen:http://codepen.io/fernandooj/pen/WROpeg 感謝您的幫助
這是你的代碼的確切副本嗎?如果是這樣,你的渲染方法中有無法訪問的代碼。在你聲明'{filteredData} = this.state;'你有'return()'包裝map函數,以便代碼停止執行並返回該地圖。作爲什麼被渲染 – finalfreq
對不起,我已經壞了渲染在這裏,並修改代碼,但仍然不會渲染 –