使用react / redux搜索/过滤列表

问题描述 投票:1回答:1

我正在学习反应和减少并且偶然发现了一个我无法理解的问题。尝试实现与本文相同的功能:https://medium.com/@yaoxiao1222/implementing-search-filter-a-list-on-redux-react-bb5de8d0a3ad但是即使来自其他api我正在使用的数据请求是成功的,我也无法将我的组件中的本地状态分配给我的redux-state,以便成为能够过滤我的结果。继承我的组成部分:

import React from 'react'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import * as fetchActions from '../../actions/fetchActions'
import Stafflist from './Stafflist'

class AboutPage extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      search: '',
      currentlyDisplayed: this.props.store.posts
    }
    this.updateSearch = this.updateSearch.bind(this)
  }
  updateSearch(event) {
    let newlyDisplayed = this.state.currentlyDisplayed.filter(
      (post) => { 
        return (
          post.name.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1
          || post.role.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1
        )}
    )
    console.log(newlyDisplayed)
    this.setState({
      search: event.target.value.substr(0, 20),
      currentlyDisplayed: newlyDisplayed
    })
  }
  render() {
     return (
      <div className="about-page">
        <h1>About</h1>
        <input type="text"
          value={this.state.search}
          onChange={this.updateSearch}
        />
        //component for rendering my list of posts.
        <Stafflist posts={this.state.currentlyDisplayed} />
      </div>
     )
  }
}
// this is here i assign my api data to this.props.store.posts
function mapStateToProps(state, ownProps) {
  return {
    store: state
  }
}

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(fetchActions, dispatch)
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(AboutPage)

比较我如何将我的商店状态分配给我的本地组件以及它在文章中的工作方式,它似乎以相同的方式完成。矿:

this.state = {
   search: '',
   currentlyDisplayed: this.props.store.posts
}

文章:

this.state = {
   searchTerm: '',
   currentlyDisplayed: this.props.people
}

反应devtools我可以看到我的数据,因为它应该在商店中,但它不能工作将其分配到组件内的本地状态,以执行过滤,我真的不知道如何调试这个。我在本地组件中的状态只是说

 State
 currentlyDisplayed: Array[0]
 Empty array

如果我改变了

<Stafflist posts={this.state.currentlyDisplayed} /> 

<Stafflist posts={this.props.store.posts} /> 

列表呈现它应该:)

减速器:

 import * as types from '../actions/actionTypes'
 import initialState from './initialState'


 export default function postReducer(state = initialState.posts, action) {
   switch(action.type) {
   case types.FETCH_POSTS_SUCCESS:
     return action.posts.data.map(post => {
       return {
         id: post.id,
         name: post.acf.name,
         role: post.acf.role
       }
     })     
   default:
     return state
   }
 }

有任何想法吗?

javascript reactjs search filter redux
1个回答
0
投票

你的代码的问题在于你没有处理如何将新收到的道具送到你的州。这意味着当您从api调用接收数据时,只更新props而组件状态不是。

如果仔细查看引用的文章,在onInputChange方法中,他们会从props重新计算状态,而updateState方法只从本地状态过滤。

在构造函数中设置状态只能确保在组件的初始安装上复制props。在那个时间点,商店只包含初始状态(reducer代码中的initialState.posts)。这是保持组件状态和存储的代价;你必须考虑如何在初始加载后保持两者同步。

一种解决方案是更新componentWillReceiveProps中的状态:

componentWillReceiveProps(nextProps){
  const nextFiltered = nextProps.store.posts.filter(your filtering code here);
  this.setState({currentlyDisplayed: nextFiltered});
}

只要组件收到新的道具,这将更新您的状态。注意反应已经淘汰了componentWillReceiveProps,你应该使用getDerivedStateToProps作为反应16.3。有关详细信息,请参阅here

© www.soinside.com 2019 - 2024. All rights reserved.