gpt4 book ai didi

reactjs - 当子组件返回新内容时,父组件是否会重新渲染?

转载 作者:行者123 更新时间:2023-12-03 13:38:13 27 4
gpt4 key购买 nike

据我所知,当父组件的 stateprops 更改时,子组件将被重新渲染。

但我不知道反之亦然的情况。

这是一个代码。

usePromise.js(定制 Hook )

import { useEffect, useReducer } from 'react';

const reducer = (state, action) => {
switch (action.type) {
case 'RESOLVED':
return { ...state, resolved: action.diff };
case 'LOADING':
return { ...state, loading: action.diff };
case 'ERROR':
return { ...state, resolved: action.diff };
default:
return state;
}
};

export default function usePromise(promiseCreator, deps = []) {
const [state, dispatch] = useReducer(reducer, {
resolved: null,
loading: false,
error: null
});

const process = async () => {
dispatch({ type: 'LOADING', diff: true });
try {
const result = await promiseCreator();
dispatch({ type: 'RESOLVED', diff: result });
} catch (e) {
dispatch({ type: 'ERROR', diff: e });
}
dispatch({ type: 'LOADING', diff: false });
};

useEffect(() => {
process();
}, deps);

return state;
}

usePromiseSample.js

import React from 'react';
import usePromise from './usePromise';

const wait = () => {
return new Promise(resolve =>
setTimeout(() => resolve('Hello hooks!'), 3000)
);
};

const UsePromiseSample = () => {
const { resolved, loading, error } = usePromise(wait);

console.log('test')

if (loading) return <div>loading...</div>;
if (error) return <div>error happened!</div>;
if (!resolved) return null;

return <div>{resolved}</div>;
};

export default UsePromiseSample;

正如您在代码上方看到的,child(usePromise.js) 组件的状态更改了四次。

但由于测试被记录了四次,父级(usePromiseSample.js)似乎也被重新渲染了四次。

如何才能轻松理解这种情况?

最佳答案

usePromise 不是子组件,而是自定义钩子(Hook)。当在 usePromise 内分派(dispatch)操作时, Hook 本身不会重新呈现,但使用它的组件会重新呈现。

如果您在另一个组件内渲染 UsePromiseSample,您将看到当 UsePromiseSample 重新渲染时,父组件不会重新渲染。

const { useEffect, useReducer } = React;

const reducer = (state, action) => {
switch (action.type) {
case 'RESOLVED':
return { ...state, resolved: action.diff, loading: false };
case 'ERROR':
return { ...state, resolved: action.diff, loading: false };
default:
return state;
}
};

function usePromise(promiseCreator, deps = []) {
const [state, dispatch] = useReducer(reducer, {
resolved: null,
loading: true,
error: null
});

const process = () => {
promiseCreator()
.then(result => {
dispatch({ type: 'RESOLVED', diff: result });
})
.catch(e => {
dispatch({ type: 'ERROR', diff: e });
});
};

useEffect(() => {
process();
}, deps);

return state;
}

const wait = () => {
return new Promise(resolve =>
setTimeout(() => resolve('Hello hooks!'), 3000)
);
};

const UsePromiseSample = () => {
const { resolved, loading, error } = usePromise(wait);

console.log('UsePromiseSample rendered')

if (loading) return <div>loading...</div>;
if (error) return <div>error happened!</div>;
if (!resolved) return null;

return <div>{resolved}</div>;
};

const App = () => {
console.log('App rendered')

return <UsePromiseSample />
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<div id="root"></div>

关于reactjs - 当子组件返回新内容时,父组件是否会重新渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55087861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com