我们可以避免使用空数组作为钩子中的第二个参数进行不必要的useEffect计算:
// that will be calculated every single re-rendering
useEffect(() => {...some procedures...})
// that will be calculated only after the first render
useEffect(() => {...some procedures...}, [])
但是,对于我们不能像上面那样做的useContext钩子,提供第二个参数。而且,我们不能用useCallback,us
如何在不重新呈现useState的情况下更改它,而只修改它的组件?我之所以需要这样做,是因为我有一些改变Comp1中的Comp1的逻辑。如果要重新呈现Comp1,就必须重新计算它的值。但是,如果改变发生了,我仍然想重新渲染它的组件。
编辑:更大的代码片段。
function Comp1() {
const [user, setUser] = useState({});
firebase.auth().onAuthStateChanged(function (_user) {
if (_user) {
// User is signed in.
setU
我正在和用户一起做一个项目。现在我正在和UserProfile一起工作
这就是我所收到的错误。
React has detected a change in the order of Hooks called by UserProfile
Previous render Next render
------------------------------------------------------
1. useState useState
2. useContext
目前我的组件渲染了4次。看起来它正在为每个道具渲染。如果我删除了除网络之外的道具,那么它只渲染一次,但是我确实需要其他道具。我在componentDidUpdate中使用了if语句,因为我需要检测更改,但是我渲染了4次,并且我的网络属性是相同的 export class Camera extends Component {
componentDidUpdate(prevProps, prevState){
if (prevProps.network !== this.props.network){
this.connect(this.props.network)
我是一个使用Okta-React进行前端开发和身份验证的新手。我有多个组件,我想在其中传递登录的用户信息,所以我使用带有钩子的react上下文。这工作得很好,但我想写一些简单的测试,但目前它只是无尽的循环。
console.error node_modules/react-dom/cjs/react-dom.development.js:88
Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect e