javascript - ReactJS setState 没有更新任何东西
问题描述
我对 reactJS 很陌生,我一直在尝试基于此https://www.florin-pop.com/blog/2019/07/sort-table-data-with-react/创建表格排序。一切正常,做了一些改变,除了我的 setState 似乎没有更新我的排序。以下是我的一些代码:
class TableView extends Component {
constructor(props) {
super(props)
this.state = {
students: [
{ name: 'Brian', age: 20, email: 'brian@hotmail.com' },
{ name: 'Kathy', age: 42, email: 'kathy@gmail.com' },
{ name: 'Donney', age: 50, email: 'donneylee@sjks.com' },
{ name: 'Cindy', age: 36, email: 'cindy@jflasjfl.com'}
]
}
this.headers = ['name', 'age', 'email'];
this.sortTypes = {
up: {
fn: (a, b) => { if (a.name < b.name) { return - 1 }}
},
down: {
fn: (a, b) => { if (a.name > b.name) { return 1 }}
},
default: {
fn: (a, b) => a
}
};
this.stateSort = {
currentSort: 'default'
};
}
onSortAsc = () => {
const { currentSort } = this.stateSort;
this.setState({ currentSort: 'up' }, () => {
console.log(currentSort);
});
}
renderTableData() {
const { currentSort } = this.stateSort;
const data = this.state.students;
return (
( data.length > 0 && (
[...data].sort(this.sortTypes[currentSort].fn).map(p => (
<tr key={p.name}>
<td>{p.name}</td>
<td>{p.age}</td>
<td>{p.email}</td>
</tr>
))
)
))
}
renderTableHeader() {
return<th key={index}>{key}<DropdownButton menuAlign="right" className="table-dropdown" title="">
<Dropdown.Item onClick={this.onSortAsc}>Sort A-Z</Dropdown.Item>
<Dropdown.Item href="#/action-2">Sort Z-A</Dropdown.Item>
</DropdownButton></th>
}
//The table
render() { //Whenever our class runs, render method will be called automatically, it may have already defined in the constructor behind the scene.
return (
<div>
<div id="table-view">
<button>Hide Fields</button>
<span class="more-btn"><FaEllipsisH /></span>
<div id="table-content">
<table id='funding-stage'>
<thead>
<tbody>
<tr>{this.renderTableHeader()}</tr>
{this.renderTableData()}
</tbody>
</thead>
</table>
</div>
</div>
</div>
)
}
};
export default Tableview;
我不确定出了什么问题。我尝试了不同的解决方案,但似乎没有任何效果。
解决方案
currentSort 状态应该在状态对象中(例如,您应该以 this.state.currentSort 访问它)如果您将它放在另一个对象( stateSort )中并且该对象是状态对象中的对象,更改它不会导致更新或重新渲染。
1 - 删除
this.stateSort = {
currentSort: 'default'
};
2 - 将 currentSort 添加到状态对象
this.state = {
students: [
{ name: 'Brian', age: 20, email: 'brian@hotmail.com' },
{ name: 'Kathy', age: 42, email: 'kathy@gmail.com' },
{ name: 'Donney', age: 50, email: 'donneylee@sjks.com' },
{ name: 'Cindy', age: 36, email: 'cindy@jflasjfl.com'}
],
currentSort: 'default'
}
3 - 在 onSortAsc 函数中
onSortAsc = () => {
this.setState({ currentSort: 'up' }, () => {
console.log(currentSort);
});
}
如果这没有帮助,请更新您的代码片段并显示整个组件以更好地帮助您。
推荐阅读
- java - 为什么 org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:(近线?
- sharepoint - 针对暂停的爬网问题的计划增量爬网行为
- json - Ajax - JSON - 初学者
- flutter - 在颤振中使用流生成器时出现错误
- typescript - 打字稿:需要帮助强制对函数响应进行严格输入
- r - 如何在ggplot中为数字x轴的条形图添加直接标签
- algorithm - 如何在 KD-Tree 中实现范围搜索
- c# - 具有预选值的 MVC 下拉列表不允许在必填字段时提交表单
- spring-boot - 在反应性 crud 存储库 @Query 方法中使用对象属性作为参数
- ios - 如何从 Callkit 最近的通话记录 iOS Objective-c 打开应用程序?