相关疑难解决方法(0)

如何在React Hooks中将道具更改为状态?

在简单的React类组件中,我们用来更改props以这种方式声明:

constructor(props) {
    super(props)

    this.state = {
      pitch: props.booking.pitch,
      email: props.booking.email,
      firstName: props.booking.firstName,
      arrivalDate: props.booking.arrivalDate
    }
} 
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何在新功能中(如Hooks)执行此操作,但是我正在尝试以这种方式执行操作。

const GenerateDescHook = ({ description: initialDesc }) => {
  const [description, setDescription] = useState(null)

useEffect(() => {
    setDescription(initialDesc)
  }, {})

 function handleChangeVariance(newVariance) {
    setDescription({
      ...description,
      template: {
        ...description.template,
        variance_name: newVariance,
      },
    })
  }

}
Run Code Online (Sandbox Code Playgroud)

基本上,我只需要更改描述道具,该道具来自另一个父组件以变为状态。请问,我能告诉我如何以新的方式像胡克斯一样去做吗?

reactjs react-hooks

17
推荐指数
2
解决办法
1万
查看次数

标签 统计

react-hooks ×1

reactjs ×1