경로 매개 변수를 변경할 때 컴포넌트가 props를 업데이트하지 않는 이유는 무엇입니까?
setState
내부 에서 사용하려고하는데 componentWillReceiveProps
불이 안 나오나요?
export default class HomePage extends React.PureComponent {
movies = require('../../movieList.json');
state = {
match: this.props.match,
id: null
}
componentWillReceiveProps(np) {
if(np.match.params.id !== this.props.match.params.id) {
console.log('UPDATING PROPS')
this.setState({ match: np.match })
}
}
render() {
const { match } = this.props;
const movie = this.movies[match.params.id || movieOrder[0]];
const nextMovie = getNextMovie(match.params.id || movieOrder[0]);
const prevMovie = getPrevMovie(match.params.id || movieOrder[0]);
return (
<>
<h1>{movie.title}</h1>
<Link to={nextMovie}>Next</Link>
<Link to={prevMovie}>Prev</Link>
</>
);
}
}
nextMovie
및 prevMovie
링크 내부 설정해야 ID를 얻을. 불행히도 첫 번째 렌더링 중에 만 설정됩니다. 클릭하면 URL 변경을 볼 수 있지만 업데이트가 실행되지 않습니다.
여기 스위치를 잡고있는 부품
export default class App extends React.PureComponent {
state = {
isLoading: true,
}
componentDidMount() {
PreloaderService.preload('core').then(() => {
console.log('LOADED ALL');
console.log('PRELOADER SERVICE', PreloaderService);
this.setState({ isLoading: false });
console.log('Preloader assets', PreloaderService.getAsset('dog-video'))
});
}
render() {
const { isLoading } = this.state;
if(isLoading) {
return(
<div>
is loading
</div>
)
}
return (
<div>
{/* <Background /> */}
<Switch>
<Route exact path="/" component={HomePage} />
<Route exact path="/:id" component={props => <HomePage {...props} />} />
<Route component={NotFoundPage} />
</Switch>
<GlobalStyle />
</div>
);
}
}```
내 codesandbox 레크리에이션 으로 작업하는 것 같습니다 .
누락 된 코드로 약간 즉흥적으로 수정해야했습니다.
의 가치는 movieOrder[0]
무엇입니까? 에 0
대한 유효한 값 match.params.id
입니까?
어떻게 getNextMovie
와 prevMovie
같은 모습?
또한 논리 Home
에서 구성 요소의 상태를 사용하지 않기 때문에 구성 요소 render
없이도 올바른 동영상을 렌더링해야한다고 생각합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다