[英]ReactJS setState doesnt update anything
Im quite new to reactJS and ive been trying to create a table sorting based on this https://www.florin-pop.com/blog/2019/07/sort-table-data-with-react/ .我对 reactJS 很陌生,我一直在尝试基于此https://www.florin-pop.com/blog/2019/07/sort-table-data-with-react/创建表格排序。 All works fine, did some changes and all, except my setState doesnt seem to update the my sorting.
一切正常,做了一些更改,除了我的 setState 似乎没有更新我的排序。 Here are some of my codes:
这是我的一些代码:
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 AZ</Dropdown.Item> <Dropdown.Item href="#/action-2">Sort ZA</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;
Im not sure what went wrong.我不确定出了什么问题。 Ive tried different solutions but nothing seems to work.
我尝试了不同的解决方案,但似乎没有任何效果。
The currentSort state should be in the state object (eg you should access it as this.state.currentSort ) if you put it in another object ( stateSort ) and that object is the one in the state object, changing it will not cause update or rerender. currentSort 状态应该在状态对象中(例如,您应该将它作为 this.state.currentSort 访问),如果您将它放在另一个对象( stateSort )中并且该对象是状态对象中的一个,更改它不会导致更新或重新渲染。
1 - remove 1 - 删除
this.stateSort = {
currentSort: 'default'
};
2 - add currentSort to state object 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 - in onSortAsc function 3 - 在 onSortAsc 函数中
onSortAsc = () => {
this.setState({ currentSort: 'up' }, () => {
console.log(currentSort);
});
}
and If that didn't help, update your code snippet and show the whole component to help you better.如果这没有帮助,请更新您的代码片段并显示整个组件以更好地帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.