【问题标题】:I want to be able to use useEffect always that I change the name of my group我希望能够始终使用 useEffect 更改我的组的名称
【发布时间】:2020-07-02 03:13:00
【问题描述】:
useEffect(() => {
  Axios.get(`https://taskcontrolapp.herokuapp.com/taskcontrol/grupo/${props.id}`)
    .then(response => {
      setGrupo(response.data)
    }).catch(error => {
      console.log(error)
    })
}, [])

我有一个对象 Grupo{ grupo_id 和 grupo_nome },我想更改 grupo_nome 并让 useEffect 再次运行,已经尝试了许多变量作为参数,但始终无法使用。

【问题讨论】:

    标签: reactjs axios react-hooks use-effect


    【解决方案1】:

    根据几篇文章和 react 文档,像这样将其放在 useEffect 中是不好的做法。

    建议您将其包装在异步的 useCallback 中,或者将其包装在 useEffect 中的函数中,然后在 useEffect 中调用该函数。

    在此处查看这些文章:

    The Abramov guy

    Official docs on custom hooks

    useEffect(() => {
      async function someFunc () {
       await Axios.get(`https://taskcontrolapp.herokuapp.com/taskcontrol/grupo/${props.id}`)
        .then(response => {
          setGrupo(response.data)
        }).catch(error => {
          console.log(error)
        })
     }
    
    someFunc()
    
     return () => {
       // do some clean up here 
     }
    
    // Note the dependency array here...
    }, [props.id, grupo_nome])
    

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      相关资源
      最近更新 更多