javascript - 无法使用react redux对数据进行排序?

标签 javascript reactjs redux react-redux

我正在尝试对数据进行排序,并在用户单击下拉按钮后以排序形式显示它。我想根据 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/

相关文章:

javascript - 在 Redux/React 组件中改变 props 值的正确方法是什么?

javascript - Meteor 上跨用户同步倒计时

javascript - Google Apps 脚本 - 如何中断循环以防止出现错误消息

html - ReactJS - 如何在 ReactJs 类组件中自动聚焦具有 contentEditable 属性 true 的元素?

javascript - 如何显示功能组件列表

javascript - 等待直到内部函数被触发

reactjs - 将规范化器与 ReduxForm 一起使用会导致表单始终脏乱

javascript - JS 为什么循环不保持相同的间隔时间?

javascript - 变量可以像对象一样对待吗?

javascript - 如何将文本放在图像上