简体   繁体   English

React Hooks:无法在 useEffect() return 中更改父级的任何内容

[英]React Hooks: Can't change anything from parent in useEffect() return

I have a component children with a form in which the user can put a tittle, description and images.我有一个带有表单的子组件,用户可以在其中放置标题、描述和图像。

This component is on a section of it's parent component, and I wan't that if the user changes of section (by error or only looking) it's form data is stored so when he comes back he won't need to write all again.该组件位于其父组件的一部分上,我不希望如果用户更改部分(通过错误或仅查看)它的表单数据将被存储,因此当他回来时,他不需要再写一遍。

I thought about storing everything directly on the parent's state, but every time the user presses a key, it would re-render the whole view.我想过将所有内容直接存储在父级的 state 上,但是每次用户按下一个键时,它都会重新渲染整个视图。

And I thought about creating a variable in the parent, that when the children with the form is rendered, its state will get it's initial state from that parent's variable, and store the information there when the component is about to unmount in useEffect(()=>{return ()=>{ props.formData = formData } },[])我考虑在父级中创建一个变量,当呈现表单的子级时,它的 state 将从该父级的变量中获取它的初始 state,并在组件即将卸载时将信息存储在那里useEffect(()=>{return ()=>{ props.formData = formData } },[])

export default class feedClase extends Component {
    data = {tittle:"", description: "", Blobs: [] }
    render(
      <>
       <FormChild data={data}/>
       <Feed/>
      </>
     )
}

FormChild({data}){
   const [tittle, setTittle] = useState(data.tittle)
   const [description, setDescription] = useState(data.description)
   const [images, setImages] = useState(data.Blobs)

   useEffect(() => {
      return () => {
         data = {Blobs: images, tittle, description}
         //I also tried storing it in the parent's state
         //-> props.setData({Blobs: images, tittle, description}) //setData={d=>this.setState({data: d})}
      }
   }, [])
   
   return(
       ... 
    )
}

But it doesn't changes anything in the parent's variables/state.但它不会改变父变量/状态中的任何内容。 How could I make it work?我怎样才能让它工作?

You'll want to store your data object inside of state.您需要将数据 object 存储在 state 内。

export default class feedClase extends Component {
    state = { data = {tittle:"", description: "", Blobs: [] } }
    render(
      <>
       <FormChild data={this.state.data}/>
       <Feed/>
      </>
     )
}

However, there's a couple things I'm noticing in your code that I'd change.但是,我注意到您的代码中有几件事我会更改。 You have state in your parent component that gets passed down as props.您的父组件中有 state 作为道具传递下来。 Then in the child component you're saving that slice of state inside of state.然后在子组件中,您将 state 的切片保存在 state 内。 You should avoid keeping two versions of state like that.您应该避免像这样保留两个版本的 state。

Also, it looks like you're trying to update your state in the useEffect hook.此外,您似乎正在尝试在 useEffect 挂钩中更新您的 state。 I'd recommend writing your handler methods in the same component as state and passing them down as props where needed.我建议将您的处理程序方法编写在与 state 相同的组件中,并在需要时将它们作为道具传递。

You can do this way:-你可以这样做: -

export default class feedClase extends Component {
data = {tittle:"", description: "", Blobs: [] }
const handleSetData = (obj) => {
    data[obj.key] = obj.value;
}
render(
  <>
   <FormChild data={data} onChange={obj => handleSetData(obj)} />
   <Feed/>
  </>
 )
}

FormChild(props){
   const [tittle, setTittle] = useState(props.data.tittle)
   const [description, setDescription] = useState(props.data.description)
   const [images, setImages] = useState(props.data.Blobs)
   const handleChange = (e) => {
       setTittle(e.target.value);
       props.onChange({key: "tittle", value: e.target.value});
   }
   return(
       <input onChange={e => handleChange(e)} placeholder="tittle" />
       ...
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM