繁体   English   中英

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

[英]searching/filtering a list with react/redux

我目前正在学习React和Redux,却遇到了一个我无法真正解决的问题。 尝试实现与本文相同的功能: 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
}

在react 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
   }
 }

有任何想法吗?

代码的问题在于您不处理如何使新收到的道具进入状态。 这意味着,当您从api调用接收数据时,仅道具会更新,而组件状态则不会。

如果您仔细查看所引用的文章,则在onInputChange方法中,它们会根据道具重新计算状态,而updateState方法仅从本地状态中进行过滤。

在构造函数中设置状态只能确保在组件的初始安装位置复制道具。 在那时,存储仅包含初始状态(Reducer代码中的initialState.posts)。 这是保持组件状态和存储的代价。 您必须考虑如何在初始加载后使两者保持同步。

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

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

每当组件收到新道具时,这将更新您的状态。 注意react已经淘汰了componentWillReceiveProps,从React 16.3开始应该使用getDerivedStateToProps。 有关更多详细信息,请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM