Я пытаюсь отсортировать данные и отобразить их отсортированную форму после того, как пользователь нажал на кнопку выпадающего меню.Я хочу отсортировать его на основе 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"
}, ..... ]