[英]ReactJS - Iterating each item (from state) in array using map. Item's property is undefined
After Python/Django I am trying my hands on ReactJS.在 Python/Django 之后,我正在尝试 ReactJS。 I was able to follow a tutorial and at least understand how ReactJS works.我能够按照教程进行操作,至少了解 ReactJS 的工作原理。 So I thought of implementing a simple scenario wherein all items form a state are shown.所以我想实现一个简单的场景,其中显示所有项目形成 state。 Upon user entry in input, the items are filtered.在用户输入输入时,项目被过滤。 That's pretty much I am trying to do.这几乎就是我想要做的。 Here is my App.js file.这是我的 App.js 文件。
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import ByLocation from './components/Bylocation';
class App extends Component {
state = {
updates : [
{
id:1,
area: 'yavatmal',
phase: 'input',
program: 'clap',
activity: 'distribution',
timestamp: '26-06-2020 14:30'
},
{
id:2,
area: 'pune',
phase: 'input',
program: 'clap',
activity: 'utilization',
timestamp: '26-06-2020 12:00'
},
],
filteredupdates : [
{
id:1,
area: 'yavatmal',
phase: 'input',
program: 'clap',
activity: 'distribution',
timestamp: '26-06-2020 14:30'
},
{
id:2,
area: 'pune',
phase: 'input',
program: 'clap',
activity: 'utilization',
timestamp: '26-06-2020 12:00'
},
]
}
onChange = (e) => {
this.setState({filteredupdates:this.state.updates})
let myUpdates = this.state.updates.filter(update => update.area.includes(e.target.value));
this.setState({filteredupdates:myUpdates})
}
render() {
return (
<div className="App">
<h3>Search By Location: </h3>
<ByLocation updates={this.state.updates} handleChange={this.handleChange}/>
</div>
);
}
}
export default App;
Here is the Bylocation component.这是 Bylocation 组件。
import React, { Component } from 'react';
import InputBox from './Inputbox';
class ByLocation extends Component {
render() {
return (
<div>
<InputBox onChange={this.props.handleChange} />
this.props.updates.map((update) => (
<p>{update.area}</p>
)
</div>
)
}
}
export default ByLocation;
And finally my last component,inputbox.最后是我的最后一个组件,输入框。
import React, { Component } from 'react';
class InputBox extends Component {
render() {
return (
<div>
<input type="text" placeholder="Enter location" />
</div>
)
}
}
export default InputBox;
I know I have to update handlechange function.我知道我必须更新 handlechange function。 But the current code refuses to run但是当前代码拒绝运行
Failed to compile./src/components/Bylocation.js Line 12:25: 'update' is not defined no-undef编译失败。/src/components/Bylocation.js 第 12:25 行:“更新”未定义 no-undef
Search for the keywords to learn more about each error.搜索关键字以了解有关每个错误的更多信息。
Why is update undefined that too in the next line?为什么在下一行中也更新未定义? That is, my map function's line where I introduce update is ok but ReactJS is complaining about update in the next line.也就是说,我引入更新的 map 函数行是可以的,但 ReactJS 在下一行抱怨更新。
Can someone help me here?有人可以在这里帮助我吗?
This code this.props.updates.map((update) => (
is treated as text, but when you add {update.area}
the curly braces, react tries to calculate the value of update.area
but it it didn't find the update
object此代码this.props.updates.map((update) => (
被视为文本,但是当您添加{update.area}
大括号时,react 尝试计算update.area
的值但它没有找到update
object
You need to wrap this.props.updates.map
in a {}
您需要将this.props.updates.map
包装在{}
Try this尝试这个
import React, { Component } from 'react';
import InputBox from './Inputbox';
class ByLocation extends Component {
render() {
return (
<div>
<InputBox onChange={this.props.handleChange} />
{ this.props.updates.map((update) => (<p>{update.area}</p>)) }
</div>
)
}
}
export default ByLocation;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.