React useEffect with property as dependency makes infinite loop

Paulo Marcio

I have a component:

const MyComp : React.FC<{ editing?: Data }> = ({editing = { title = '', description = '', date: new Date() } }) => {
    const [data, setData] = useState<Data>({...editing})
    useEffect(() => setData({...editing}), [editing])
    return (/* use data inside here to render component */)
}

The problem is that the useEffect is looping, so React is detecting that the editing prop is changing every, the thing is that i use MyComp without any props like this:

<MyComp></MyComp>

This is really confusing me, i'm kinda lost now on how React works, any ideas on why is this happening?

Alexander

Because editing is an object. Objects are compared by reference. If you don't pass the prop editing to the component, in each render, editing will receive a new link in memory, because you passing a default value to it. So useEffect will assume that dependencies have changed.

You can set primitive types to dependencies.

const MyComp : React.FC<{ editing?: Data }> = ({editing = { title = '', description = '', date: new Date() } }) => {
    const [data, setData] = useState<Data>({...editing})
    useEffect(() => setData({...editing}), [editing.title, editing.description, editing.date.getTime()])
    return (/* use data inside here to render component */)
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

React TypeScript 16.8 How to add a dependency to useEffect()

分類Dev

React simple fetch program run into an infinite loop

分類Dev

Infinite loop when using react hooks

分類Dev

React UseState hook causing infinite loop

分類Dev

Correct/Incorrect to ignore some React useEffect dependency warnings?

分類Dev

Why does this create an infinite render loop with React hooks?

分類Dev

Why does setState cause my React app go into infinite loop?

分類Dev

React Hook useEffect has a missing dependency when passing empty array as second argument

分類Dev

React giving me Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

分類Dev

Stuck on infinite if loop

分類Dev

Running into infinite loop with scanf

分類Dev

Prolog infinite loop issue

分類Dev

Regex Infinite Loop

分類Dev

Singleton going into an infinite loop

分類Dev

Memory leak in infinite loop

分類Dev

While loop creating infinite loop

分類Dev

React js useEffect

分類Dev

React Native Infinite Scroll

分類Dev

React infinite rerender、useEffectの問題、設定された状態がトリガーされない

分類Dev

"Infinite loop" causing unreachable code

分類Dev

Golang: goroutine infinite-loop

分類Dev

Infinite loop with recursive javascript functions

分類Dev

Infinite loop with recursive javascript functions

分類Dev

Infinite loop, page unable to load

分類Dev

My code generates an infinite loop

分類Dev

How to bystep this $watch infinite loop?

分類Dev

Infinite loop inside a thread not working

分類Dev

an infinite loop inside a javascript game

分類Dev

JS Bin while infinite loop

Related 関連記事

ホットタグ

アーカイブ