[英]Trying to fix my react class component. Getting the error "Expected an assignment or function call and instead saw an expression"
I'm getting the error message:我收到错误消息:
Line 4:2: Expected an assignment or function call and instead saw an expression第 4:2 行:期望赋值或函数调用,但看到的是表达式
I think I wrote this class component the same way I've written them in the past?我想我写这个类组件的方式与我过去编写它们的方式相同? Not sure what I did wrong.不知道我做错了什么。 Any help would be appreciated.任何帮助,将不胜感激。
import React, { Component } from 'react'
import Person from './Components/Person'
class App extends Component {
state = {
people: [
{name: 'David', age: '30'},
{name: 'Michael', age: '27'},
{name: 'John' , age: '33'}
],
bool: true
}
clickHandler = () => {
const currentBool = this.state.bool
this.setState({
bool: !currentBool
})
}
render () {
let renderDiv = null
if (this.state.bool) {
renderDiv = (
<div>
{this.state.people.map(person => {
return <Person name={person.name} age={person.age} />
})}
</div>
)
}
return (
<div>
<button onClick={this.clickHandler}>show/hide</button>
{renderDiv}
</div>
)
}
}
export default App
I think your code looks good.我认为你的代码看起来不错。 You'd see that linting error if you didn't have a return
in the statement return <Person name={person.name} age={person.age} />
but since you already have it, it should be good.如果您在return <Person name={person.name} age={person.age} />
语句中没有return
,您会看到 linting 错误,但既然您已经有了它,它应该很好。
But looking at the line number you provided, it doesn't look like that lint error is coming from this file.但是查看您提供的行号,看起来 lint 错误并非来自此文件。 Could that be coming from your Person
component?这可能来自您的Person
组件吗? You haven't provided the code for it, so just an assumption.你还没有提供它的代码,所以只是一个假设。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.