docs는 말한다 :상위 컴포넌트에서 componentDidMount가 두 번 이상 호출 된 이유는 무엇입니까?
초기 렌더링이 발생한 후 즉시 만 (가 아닌 서버) 클라이언트에 한 번 불려갑니다. 이제
, 나는 고차 구성 요소를 만들려고 할 때 : 나는 경우
'wrapped component mounted'
'wrapper component mounted'
: 지금 어떤 변화가 소품 발생할 때마다
import React from 'react';
import { connect } from 'react-redux';
function wrap(Wrapped) {
class Wrapper extends React.Component {
componentDidMount() {
// I will place some reusable functionality here which need to
// be called once on mounted.
console.log('wrapper component mounted');
}
render() {
return <Wrapped {...this.props}/>
}
}
return Wrapper;
}
class Wrapped extends React.Component {
componentDidMount() {
console.log('wrapped component mounted');
}
render() {
return <div></div>;
}
}
connect()(wrap(Wrapped));
을 콘솔이 인쇄됩니다 Wrapper
을 제거하면 첫 번째로 으로 마운트 된 경우에만 한 번만 인쇄됩니다.
`wrapped component mounted`
왜 컴포넌트 구성 요소의 상위 구성 요소에서 componentDidMount가 두 번 이상 호출 되었습니까?
connect() (줄 바꿈 (래퍼)); <- Wrapped로 대체하십시오. –
@ffxsam 죄송합니다. 귀하의 의견을 제대로 이해하지 못하면 제 영어 실력이 좋지 않습니다. 하지만 잘못하지 않았다면 컴포넌트 ('Wrapper'와'Wrapped')가 이미 마운트되어있을 때'componentDidMount'는 마운트 해제되지 않은 한 다시 호출되지 않습니다. 그리고 여기서 제가 말하고자하는 것은 두 개의'componentDidMount'는 소품이 바뀔 때마다 항상 호출된다는 것입니다. –
오, 죄송합니다! 소품 변경으로 인해'componentDidMount'가 다시 발동되는 부분을 놓쳤습니다. 아주 이상한. – ffxsam