相关疑难解决方法(0)

使用useEffect React Hook时如何解决缺少依赖项警告?

使用React 16.8.6(在以前的版本16.8.3中很好),当我尝试防止在获取请求上发生无限循环时,出现此错误

./src/components/BusinessesList.js
Line 51:  React Hook useEffect has a missing dependency: 'fetchBusinesses'.
Either include it or remove the dependency array  react-hooks/exhaustive-deps
Run Code Online (Sandbox Code Playgroud)

我一直找不到停止无限循环的解决方案。我想远离使用useReducer()。我确实在https://github.com/facebook/react/issues/14920找到了这个讨论,在这里可能的解决方案是You can always // eslint-disable-next-line react-hooks/exhaustive-deps if you think you know what you're doing.我不确定自己在做什么,所以我还没有尝试实现它。

我有这个当前设置,React hook useEffect永远/无限循环连续运行,唯一的注释是useCallback()我不熟悉的。

我目前的使用方式useEffect()(类似于,我一开始只想运行一次componentDidMount()

useEffect(() => {
    fetchBusinesses();
  }, []);
Run Code Online (Sandbox Code Playgroud)
const fetchBusinesses = () => {
    return fetch("theURL", {method: "GET"}
    )
      .then(res => normalizeResponseErrors(res))
      .then(res => {
        return res.json();
      }) …
Run Code Online (Sandbox Code Playgroud)

reactjs eslint create-react-app react-hooks

58
推荐指数
12
解决办法
5万
查看次数

我们如何知道 React ref.current 值何时发生了变化?

正常情况下,有了props,我们就可以写

componentDidUpdate(oldProps) {
  if (oldProps.foo !== this.props.foo) {
    console.log('foo prop changed')
  }
}
Run Code Online (Sandbox Code Playgroud)

为了检测道具的变化。

但是如果我们使用React.createRef(),我们如何检测 ref 何时更改为新组件或 DOM 元素?React 文档并没有真正提及任何内容。

铁,

class Foo extends React.Component {
  someRef = React.createRef()

  componentDidUpdate(oldProps) {
    const refChanged = /* What do we put here? */

    if (refChanged) {
      console.log('new ref value:', this.someRef.current)
    }
  }

  render() {
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我们是否应该自己实现某种旧价值的东西?

铁,

class Foo extends React.Component {
  someRef = React.createRef()
  oldRef = {}

  componentDidMount() {
    this.oldRef.current = this.someRef.current
  }

  componentDidUpdate(oldProps) {
    const …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-ref

24
推荐指数
2
解决办法
4万
查看次数

如何结交Fabric.js和Redux?

有没有什么方法可以将Fabric.js和Redux一起使用?Fabric.js状态应该用作存储的一部分,但它不是不可变的,并且可以通过用户画布交互来改变自身.任何的想法?谢谢.

fabricjs redux

5
推荐指数
2
解决办法
1827
查看次数