【问题标题】:My component isn't updating, am I mutating the state?我的组件没有更新,我在改变状态吗?
【发布时间】:2016-10-20 17:52:38
【问题描述】:

我有一个容器组件,它连接到我用 immutable.js 创建的状态。当我更新状态时,我的 redux 检查器告诉我状态已更新,但我的组件没有得到新的更新,也没有重新渲染。

我的容器组件:

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { setCategoryActive } from '../actions'
import Category from '../components/Category'

class MenuList extends Component {


  constructor(props) {
    super(props)
    this.categories = this.props.categories
    this.subreddits = this.props.subreddits
    this.allCategories = this.categories.get("allCategories")
    this.byName = this.categories.get("byName")
  }

  printSubreddits(category) {
    const subreddits = this.byName.get(category)
    const subredditsByName = subreddits.get("subredditsByName")

    const list = subredditsByName.map((subreddit, i) => {
      return <p className="swag" key={i}>{subreddit}</p>
    })

    return list
  }

  isCategoryActive(category) {
    const cat = this.byName.get(category)
    return cat.get("active")
  }

  printCategory(category, i) {
    console.log(this.isCategoryActive(category))
    return (
      <div className="category-container" key={i}>
        <Category name={category}
          active={this.isCategoryActive(category)}
          setActive={this.props.setCategoryActive.bind(this, category)} />
        {this.isCategoryActive(category) ? this.printSubreddits(category) : null}
      </div>
    )
  }


  render() {

    return (
      <div>
        {this.allCategories.map((category, i) => {
          const x = this.printCategory(category, i)
          return x
        }, this)}
      </div>
    )
  }
}

const mapStateToProps = (state) => ({
  categories: state.subredditSelector.get('categories'),
  subreddits: state.subredditSelector.get('subreddits')
})

export default connect(mapStateToProps, {
  setCategoryActive
})(MenuList);

我的类别组件

class Category extends Component {

  printToggle(active) {
    if (active) {
      return <span> [-]</span>
    } else {
      return <span> [+]</span>
    }
  }

  componentWillReceiveProps(nextProps) {
    this.printToggle(nextProps.active)
  }

  render() {

    const { setActive, active, name } = this.props

    return (
      <div className="category-container">
        <a onClick={setActive}
        href="#"
        className="category-title">
          {name}
          {this.printToggle(active)}
        </a>
      </div>
    )
  }

}

export default Category

还有我的减速器

import { fromJS } from 'immutable'
import {
  SET_SUBREDDIT_ACTIVE,
  SET_CATEGORY_ACTIVE
} from '../actions'
import List from '../data/nsfw_subreddits.js'

const initState = fromJS(List)

const subredditSelector = (state = initState, action) => {
  switch (action.type) {
    case SET_SUBREDDIT_ACTIVE:
      return state
    case SET_CATEGORY_ACTIVE:
      return state.updateIn(['categories', 'byName', action.payload.name],
        x => x.set('active', !x.get('active')))
    default:
      return state
  }
}

export default subredditSelector

我已编码为 JSON 对象的状态片段

const list = {
  categories: {
    byName: {
      "example": {
        name: "example",
        id: 1,
        active: true,
        subredditsByName: ["example", "example"]
      },
      "example": {
        name: "example",
        id: 2,
        active: true,
        subredditsByName: ["example"]
      },
      "example": {
        name: "example",
        id: 3,
        active: true,
        subredditsByName: ["example", "example", "example"]
      }
    },
    allCategories: ["example", "example", "example"]
  },
  subreddits: {

我的猜测是我的减速器正在改变状态?虽然我不确定如何,因为我使用的是不可变的 js 函数?

【问题讨论】:

  • 嗨 Albert,我只是想在这里仔细检查一下 - 您是否希望“我的容器组件”在您的状态更改时重新渲染 AllCategories?如果是这样,那么我认为这里的问题是 this.allCategories 在您的构造函数中只设置一次,并且引用永远不会改变。我建议将 allCategories 添加到您的 mapStateToProps 方法中。我现在无法自己测试这个,所以我可能不正确,但我认为这种方法值得一试!

标签: reactjs redux immutable.js


【解决方案1】:

所以我通过将构造函数从构造函数更改为常规函数并在我的渲染方法顶部调用它来解决此问题!

【讨论】:

  • 根据 prop 更改更新组件的更好地方是 componentWillReceiveProps。将这些东西放入 render 意味着它被调用的频率比必要的要高,这对你的组件可能无关紧要。
【解决方案2】:

你仍然应该保留你的构造函数,但只包含第一次创建对象时需要做的事情:

constructor(props) { super(props) }

是的,拥有

cleanPropData(){ this.categories = this.props.categories this.subreddits = this.props.subreddits this.allCategories = this.categories.get("allCategories") this.byName = this.categories.get("byName") }

很好。我还没有听说有人在渲染函数的顶部调用它。很高兴知道这行得通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2021-10-19
    • 2019-08-08
    相关资源
    最近更新 更多