setState React问题

时间:2018-12-10 14:42:47

标签: javascript reactjs state

我在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语句。 谢谢您的帮助!

0 个答案:

没有答案