【问题标题】:React component doesn't update after updating state with redux action使用 redux 操作更新状态后,React 组件不更新
【发布时间】:2019-08-18 02:07:03
【问题描述】:

/keywordsActions

import { UPDATE_KEYWORDS } from "./actionTypes";
import queryString from "query-string";

const keywordsArrayFromUrl = () => {
  const query = queryString.parse(window.location.search);
  if (query.keywords) {
    const removeDuplicate = new Set(query.keywords.split(" "));
    return Array.from(removeDuplicate);
  }

  return [];
};

export function updateKeywords() {
  return async dispatch => {
    dispatch({
      type: UPDATE_KEYWORDS,
      payload: await keywordsArrayFromUrl()
    });
  };
}

/keywordReducer

import { UPDATE_KEYWORDS } from "../actions/actionTypes";

export default function(state = [], action) {
  switch (action.type) {
    case UPDATE_KEYWORDS:
      return action.payload;
    default:
      return state;
  }
}

/SearchBar -- React 组件

import React, { Component } from "react";
import { withRouter } from "react-router-dom";
//Redux
import { connect } from "react-redux";
import { updateKeywords } from "../store/actions/KeywordsAction";

class Searchbar extends Component {
  constructor(props) {
    super(props);
    this.state = {
      keywords : this.props.keywords
      keywordsString: this.props.keywords.join(" ")
    };
  }

  componentDidMount() {
    this.props.updateKeywords();
    console.log(this.props)
    setTimeout(() => console.log(this.props), 10);
  }

  _handleChange = e => {
    this.setState({ keywordsString: e.target.value });
  };

  _handleSearch = value => {
    this.setState({ keywordsString: value });
    this.props.history.push(`/search?keywords=${value}`);
  };

  render() {
    return (
      <Search
        className="Searchbar"
        placeholder="Cauta prin iBac..."
        value={this.state.keywordsString}
        onChange={this._handleChange}
        onSearch={this._handleSearch}
      />
    );
  }
}

const mapStateToProps = state => {
  return {
    keywords: state.keywords
  };
};

export default connect(
  mapStateToProps,
  { updateKeywords }
)(withRouter(Searchbar));

我想将 Url 中的关键字保存到商店,然后将其传递到搜索栏状态。

但我不明白这一点:

  componentDidMount() {
    this.props.updateKeywords();
    console.log(this.props); // this.props.keywords is empty
    setTimeout(() => console.log(this.props), 10); // After 10 ms this.props.keywords is no empty 
  }

10 毫秒后,Searchbar 的 props 会更新,但组件不会再次呈现。

抱歉我的问题,我对 React / Redux 真的很陌生。请让我知道我做错了什么。谢谢大家!

更新:

  componentDidMount() {
    this.props.updateKeywords();
    setTimeout(() => {
      this.setState({
        keywordsString: this.props.keywords.join(" ")
      });
    }, 0);
  }

这个代码也可以工作...但是另一个不工作

componentDidMount() {
    this.props.updateKeywords();
      this.setState({
        keywordsString: this.props.keywords.join(" ")
      });
  }

【问题讨论】:

    标签: reactjs redux react-redux react-thunk


    【解决方案1】:

    原因是componentDidMount 在挂载时只被调用一次。您正在寻找的是componentShouldUpdatecomponentDidUpdaterender 函数,当您的组件从redux 接收到更新的状态时,所有这些都会被调用。您可以在此处阅读以了解有关这些函数的作用的更多信息。

    https://reactjs.org/docs/react-component.html#updating

    【讨论】:

    • 不,这不能解决问题......但谢谢你的回答!
    • 您是否尝试在您的渲染函数中记录this.props.keywords?它改变了价值吗?
    猜你喜欢
    • 2019-12-29
    • 2022-08-17
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多