javascript - 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 语句。感谢您的帮助!
解决方案
推荐阅读
- python - 如何将祖父文件夹名称附加到文件名?
- javascript - 如何根据选择标签的选定选项的值为变量分配新值,检索并使用变量的新值?
- python - KaToolin 似乎只是将这个独特的密钥硬编码为可以工作,我们如何判断它是否合法地与真正的 Kali 项目相关联?
- scala - 将数字类型转换为不带参数的 Double
- sql-server - Tomcat 应用程序在迁移到 Azure 上的 PaaS 平台时运行速度变慢
- maven - 如何在 Maven 构建期间将外部依赖项复制到 Open Liberty
- php - 尝试在 php 中使用正则表达式查找标签并获取属性
- laravel - Laravel - 运行排队作业时 $_SERVER['DOCUMENT_ROOT'] 为空?
- python - 我在 python 中实现 OOP 时遇到了挑战
- reactjs - 如何从 REACTJS 中的路由获取参数