我对 ReactJs 很陌生,我正试图了解更多细节。
在类组件中,我知道这两种方式可以声明处理程序方法来更改状态。
classChangeState=()=>{
this.setState({
Person:[
{firstName:"test", secondName:55}
]
})
}
classChangeState2(){
console.log("Inside Change state click")
this.setState({
Person:[
{firstName:"test", secondName:55}
]
})enter code here
//classChangeState2 require me to bind "this" inside render method
在功能组件中,以下两种方式我都可以做到
function changeStateClick(){
changeState({
Person:[
{firstName: "Just aasefaesfstring property", secondName: 32}
]
})
}
const changeStateClick2 =()=> changeState({
Person:[
{firstName: "Just a[[[string property", secondName: 32}
]
})
我有几个问题 1) React 如何知道类ChangeState2 是一个没有“函数”的方法?
2)我知道我可以在上述所有方法中传入newName作为参数,但我必须在所有方法的渲染中绑定“THIS”。例如,对于方法名称.bind(this,“newNamehere”)
这是为什么呢?即使对于功能组件,当我想要添加“newName”作为参数时,最初我不需要绑定,我现在也必须绑定。有人可以解释一下吗?
classChangeState=(newName)=>{
this.setState({
Person:[
{firstName:newName, secondName:55}
]
})
}
蛊毒传说
汪汪一只猫
随时随地看视频慕课网APP
相关分类