标题react开发中遇到的问题
1.子组件像父组件传值
在父组件中
const valueRef = useRef()
const handleEditOk = () =>{
// value = this.ref['children'].state.value
console.log(valueRef.current.state.value)//传给后台
console.log(value)
setEdit({
edit:false
})
// console.log(edited.edit)
}
< editState res={companyState} ref={valueRef}/>
函数组件中,官网给的是用useRef来使用ref,在子组件中绑定这个ref,然后就可以获取子组件中state的值
export default class EditState extends React.Component{
constructor(props){
super(props)
this.state=({
value:' hello world'
})}
2.使用antD中的modal弹窗时,弹窗内容只渲染一次,为了每次获取新的数据,所以需要弹窗关闭后销毁子组件,使用modal中的destroyOnClose=‘true’来销毁
<Modal
title='状态编辑'
visible={edited.edit}
onOk={handleEditOk}
onCancel={handleEditCancel}
okText='确认'
cancelText='取消'
destroyOnClose='true'
>
<EditState res={companyState} ref={valueRef}/>
{/* 关闭之后要销毁这个子组件 */}
</Modal>