javascript - 单击按钮时打印 TextField - 打印不正确
问题描述
我正在尝试在我的反应应用程序中合并一个功能,用户将在其中输入搜索词,当按下按钮时,它会搜索它。(文本输入和按钮正在使用 material-ui 组件)现在我只想让它将术语打印到控制台日志以检查它是否正常。运行以下代码时,输出为:“[object Object]。有谁知道这是为什么?
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
class searchText extends React.Component {
state = {
searchTerm: null,
}
handleClick = (searchTerm) => {
this.setState({ searchTerm });
}
render() {
const { searchTerm } = this.state;
return (
<div className ="Search">
<TextField
id = "outlined-search"
label="Enter Query"
type = "search"
className ="Search"
variant="filled"
value = {searchTerm}
/>
<Button
onClick={() =>{console.log("search: " +{searchTerm}); }}
varient ="contained"
classname="goButton" >
Enter Query Here!
<Button/>
</div>
);
}
}
导出默认搜索
更新:修正错别字
解决方案
您没有任何功能来获取输入值并将该值设置为状态。尝试这个。
// function for TextField Value Change
getvalue=(event)=>{
this.setState({
searchTerm:event.target.value
})
}
// function for button click
handleClick = () => {
console.log(this.state.searchTerm)
}
<TextField
id = "outlined-search"
label="Enter Query"
type = "search"
className ="Search"
variant="filled"
value = {this.state.searchTerm}
onChange={this.getvalue}
/>
推荐阅读
- python-3.x - ModuleNotFoundError:没有带有屏幕截图实用程序的名为“fcntl”的模块
- python - 合并两个大数据框
- go - 采用 map 且只关心键类型的函数
- sql-server - 从 Sql Server 调用 Web 服务 - SSL 问题
- kubernetes - 从 GCP 中删除 Kubernetes 集群失败
- c# - Graphql 过滤子项
- dart - AppLifcycleState.didChangeLifecycleState( ) 函数在应用程序进入前台或后台时不被调用
- download - 使用 Postman 将文件下载到特定位置
- ionic-framework - 无法在 ionic 3 中执行“createObjectURL”
- azure - 将 authorization.json 与 Azure Functions 结合使用会破坏门户