我正在尝试对数据进行排序,并在用户单击下拉按钮后以排序形式显示它。我想根据 funds
对其进行排序即整数值。所以我添加了onClick
上<a>
标签,但它不起作用,为什么会这样?
home.js:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { sortByFunded } from '../../store/actions/sortAction';
class Home extends Component {
render() {
const { searchTermChanged, sortByFunded } = this.props;
return (
<div>
<div className="buttonContainer">
<div>
<button className="btn btn-primary mycustom dropdown-toggle mr-4" type="button" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">Sort by </button>
<div className="dropdown-menu">
<a className="dropdown-item" href="#">End time</a>
<a className="dropdown-item" href="#" onClick={sortByFunded}>Percentage fund</a>
<a className="dropdown-item" href="#">Number of backers</a>
</div>
</div>
</div>
</div>
)
}
}
const mapStateToProps = state => ({
projects: state.search.projects,
sort: state.sort.projects
})
export default connect(mapStateToProps, { searchTermChanged, sortByFunded })(Home);
sortAction.js:
import { SORT_BY_FUNDED } from './types';
export const sortByFunded = () => ({
type: SORT_BY_FUNDED
});
sortReducer.js:
import { SORT_BY_FUNDED } from '../actions/types';
import Projects from '../../data/projects';
const initialState = {
projects: Projects
}
export default function (state = initialState, action) {
switch (action.type) {
case SORT_BY_FUNDED:
return {
...state,
projects: Projects ? Projects.sort((a, b) => a.funded - b.funded) : Projects
};
default:
return state;
}
}
projects.js:
export default [
{
"s.no":0,
"amt.pledged":15823,
"blurb":"'Catalysts, Explorers & Secret Keepers: Women of Science Fiction' is a take-home exhibit & anthology by the Museum of Science Fiction.",
"by":"Museum of Science Fiction",
"country":"US",
"currency":"usd",
"endTime":"2016-11-01T23:59:00-04:00",
"location":"Washington, DC",
"funded":186,
"backers":"219382",
"state":"DC",
"title":"Catalysts, Explorers & Secret Keepers: Women of SF",
"type":"Town",
"url":"/projects/1608905146/catalysts-explorers-and-secret-keepers-women-of-sf?ref=discovery"
},
{
"s.no":1,
"amt.pledged":6859,
"blurb":"A unique handmade picture book for kids & art lovers about a nervous monster who finds his courage with the help of a brave little girl",
"by":"Tyrone Wells & Broken Eagle, LLC",
"country":"US",
"currency":"usd",
"endTime":"2016-11-25T01:13:33-05:00",
"location":"Portland, OR",
"funded":8,
"backers":"154926",
"state":"OR",
"title":"The Whatamagump (a hand-crafted story picture book)",
"type":"Town",
"url":"/projects/thewhatamagump/the-whatamagump-a-hand-crafted-story-picture-book?ref=discovery"
}, ..... ]
最佳答案
在我当前的实现中,我实际上并未将项目存储在您的 reducer 中,而是在排序时始终从源中提取它。还对其进行变异。
工作代码:
reducer .js:
import { SORT_BY_FUNDED } from '../actions/types';
import Projects from '../../data/projects';
const initialState = {
projects: Projects
}
export default function (state = initialState, action) {
switch (action.type) {
case SORT_BY_FUNDED:
return {
...state,
projects: state.projects.length > 0 ? [...state.projects.sort((a,b) => a.funded - b.funded)] : state.projects
};
default:
return state;
}
}
关于javascript - 无法使用react redux对数据进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932642/