首页 > 解决方案 > setState 反应问题

问题描述

我对 React 组件中的 setState 有疑问。我正在尝试在componentDidMount方法中更新 advSearch 的状态 - 但状态根本没有改变。我在这里发送代码:

class BookList extends Component {
state = {
books: [],
advSearch: {
  author: "",
  title: "",
  category: "",
  year: "",
  isbn: ""
}
};

componentDidMount() {


console.log(this.props.match.path.substring(1, 9));
if (this.props.match.path.substring(1, 9) === "advanced") {
  this.setState({
    advSearch: {
      author: this.props.match.params.author,
      title: this.props.match.params.title,
      category: this.props.match.params.category,
      year: this.props.match.params.year,
      isbn: this.props.match.params.isbn
    }
  });

在我的场景中,代码通过了上面的 if 语句。感谢您的帮助!

标签: javascriptreactjsstate

解决方案


推荐阅读