reactjs – 如何在React Hooks中将道具改为状态?
发布时间:2020-12-15 16:20:26 所属栏目:百科 来源:网络整理
导读:在Simple react类组件中,我们用这种方式将props更改为: constructor(props) { super(props) this.state = { pitch: props.booking.pitch,email: props.booking.email,firstName: props.booking.firstName,arrivalDate: props.booking.arrivalDate }} 但我不
在Simple react类组件中,我们用这种方式将props更改为:
constructor(props) { super(props) this.state = { pitch: props.booking.pitch,email: props.booking.email,firstName: props.booking.firstName,arrivalDate: props.booking.arrivalDate } } 但我不知道如何在Hooks这样的新功能中做到这一点,但我试图这样做. const GenerateDescHook = ({ description: initialDesc }) => { const [description,setDescription] = useState(null) useEffect(() => { setDescription(initialDesc) },{}) function handleChangeVariance(newVariance) { setDescription({ ...description,template: { ...description.template,variance_name: newVariance,},}) } } 基本上,我只需要更改来自另一个父组件的描述道具,以转向状态.请问,你能告诉我如何以Hooks的方式以新的方式做到这一点吗? 解决方法
您可以将初始状态作为第一个参数传递给useState,如下所示:
const GenerateDescHook = ({ description: initialDesc }) => { const [description,setDescription] = useState(initialDesc) ... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |