【发布时间】:2023-03-16 22:23:02
【问题描述】:
- 我是 React 应用程序的新手
- 我正在尝试在 UI 中呈现过滤后的对象值数组
- 我尝试在 onClick 函数中使用 setState 以及条件渲染的概念
- 但在单击提交按钮后,我无法在邮政编码匹配文本字段中呈现 newArray 值
- 你能帮助大家就这个问题提供任何可能的建议
- 我的完整代码可在以下链接中找到 - 提供下面的代码 sn-p: https://stackblitz.com/edit/react-geum6v?file=index.js
index.js
Players Belonging to same zip code: <input type="text" name="zip_code" defaultValue = {this.state.zip1} onChange={this.handleChange}></input>
<button onClick = {this.sports_zip_search}>Submit</button><br />
{this.state.zip_value && <Zip />}
sports_zip_search = () => {
var newArray = this.state.student.filter((el) => {
return el.zip == this.state.zip1 });
console.log(newArray);
this.setState ({result: newArray})
//console.log("result", this.state.result);
this.state.zip_value = true;
//return (<h1>newArray</h1>)
}
Zip.js
const Zip = (props) => {
return(
<h1>{ props.result }</h1>
)
}
【问题讨论】:
-
您在
Zip中使用了 props.result,但在渲染组件时未将任何 props 传递给组件,也未提供任何默认值
标签: javascript html reactjs