内容在排序后不会改变,React。

huangapple go评论85阅读模式
英文:

content doesn't change after sorting react

问题

我有一个小的React应用。我想要实现按不同参数排序的功能,当您选择一个sortType时,下面的内容将根据这些标准进行排序。

我创建了一个函数,应该根据sortType返回带有必要参数的数据,但它不起作用。内容没有改变。
我认为这里可能有些错误,但我没能注意到,如果有人能帮助我,我会很高兴。

英文:

I've a small React app. I want to make it possible to sort by different parameters, when you select a sortType, the content below is sorted by those criteria.

I created a function that should return data already with the necessary parameters depending on the sortType, but it doesn't work. The content doesn't change.
I think that there is some kind of mistake here and I can’t notice it, I'll be glad if someone helps me with this

  1. function MarkersPage() {
  2. const markers = useSelector((state) => state.data.markers);
  3. const [data, setData] = useState([...markers]);
  4. const [sortType, setSortType] = useState(1);
  5. const markerItem = (item) => {
  6. return (
  7. <div className="markers-item" key={item.id}>
  8. <div className="top-info">
  9. <div className="icon">
  10. <img
  11. src={
  12. item.isHome
  13. ? "../assets/home-marker.png"
  14. : "../assets/default-marker.png"
  15. }
  16. alt=""
  17. />
  18. </div>
  19. <div className="coords">
  20. <span>{item.coordinates[0].toFixed(5)}, </span>
  21. <span>{item.coordinates[1].toFixed(5)}</span>
  22. </div>
  23. <div className="btn-block">
  24. <button type="button" onClick={() => handleEdit(item)}>
  25. edit
  26. </button>
  27. <button
  28. type="button"
  29. onClick={() => dispatch(deleteMarker(item.id))}
  30. >
  31. delete
  32. </button>
  33. </div>
  34. </div>
  35. <div className="main-info">
  36. <div className="info">
  37. <h6>{item.title}</h6>
  38. </div>
  39. {item.note ? (
  40. <details className="note">
  41. <summary>Note:</summary> <p>{item.note}</p>
  42. </details>
  43. ) : (
  44. ""
  45. )}
  46. </div>
  47. <div className="bottom-info">
  48. <p className="postcode">{item.postcode}</p>
  49. <p className="country">{item.country}</p>
  50. </div>
  51. </div>
  52. );
  53. };
  54. const dataChanges = () => {
  55. if (sortType === 1) {
  56. console.log(1);
  57. return data.map((item) => {
  58. return markerItem(item);
  59. });
  60. }
  61. if (sortType === 2) {
  62. console.log(2);
  63. return data.map((item) => {
  64. return markerItem(item);
  65. });
  66. }
  67. if (sortType === 3) {
  68. console.log(3);
  69. return data
  70. .sort((a, b) => a.date - b.date)
  71. .map((item) => {
  72. return markerItem(item);
  73. });
  74. }
  75. if (sortType === 4) {
  76. console.log(4);
  77. return data
  78. .sort((a, b) => b.date - a.date)
  79. .map((item) => {
  80. return markerItem(item);
  81. });
  82. }
  83. };
  84. return (
  85. <div className="markerspage">
  86. <div className="container">
  87. <div
  88. className="markerspage-inner"
  89. style={editMode ? { filter: "blur(10px)" } : {}}
  90. >
  91. <div className="sort-bar">
  92. <select
  93. defaultValue={1}
  94. onChange={(event) => setSortType(event.target.value)}
  95. >
  96. <option value="1">All</option>
  97. <option value="2">Home</option>
  98. <option value="3">Date ↑</option>
  99. <option value="4">Date ↓</option>
  100. </select>
  101. </div>
  102. <div>
  103. {!markers ? (
  104. <div>No places</div>
  105. ) : (
  106. <div className="markers-items">{dataChanges()}</div>
  107. )}
  108. </div>
  109. </div>
  110. </div>
  111. </div>
  112. );
  113. }
  114. export default MarkersPage;

答案1

得分: 1

你需要在你的 dataChanges 中调用 setData(data…),而不是 return data…

此外,我建议你使用 Redux Toolkit 中的 createSelector,这样你可以像这样做:

  1. const sortedData = useSelector((state) => sortedDataSelector(state, sortType))
  1. const sortedDataSelector = createSelector(
  2. (state, sortType) => state.data.markers,
  3. (state, sortType) => sortType,
  4. (markers, sortType) => {
  5. if (sortType === 1)
  6. return markers.sort()
  7. }
  8. )

Selectors 有点棘手,你可以从这里开始学习:https://redux.js.org/usage/deriving-data-selectors#createselector-overview

英文:

You have to call setData(data…) instead of return data… inside your dataChanges

however I can recommend using createSelector from redux toolkit so then you can do something like that:

  1. const sortedData = useSelector((state) => sortedDataSelector(state, sortType)
  1. const sortedDataSelector = createSelector(
  2. (state, sortType) => state.data.markers,
  3. (state, sortType) => sortType,
  4. (markers, sortType) => {
  5. if (sortType === 1)
  6. return markers.sort(…)
  7. }
  8. )

Selectors are a bit tricky you can start from here https://redux.js.org/usage/deriving-data-selectors#createselector-overview

huangapple
  • 本文由 发表于 2023年5月7日 18:03:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定