제목에 따라 초기 this.props가 실패하는 이유는 무엇입니까? 그리고 더 실질적으로 생성자 내에서 소품에 의존하는 경우이 문제를 어떻게 해결합니까? 예를 들어 구독 내에서 소품을 참조하고 싶습니까?
class AboutBox extends Component {
static defaultProps = {
title: 'Undefined Product',
}
constructor() {
super();
console.log(this.props.title); //this fails (=> null)
}
render() {
console.log(this.props.title); //this works (=> 'Undefined Product')
return null;
}
}
생성자 args에 소품을 넣고 super에 전달해야합니다.
constructor(props){
super(props);
console.log(props.title);
}
생성자 내부에서 소품을 사용하면 작동하지만 IE에 대한 변환에 약간의 문제가 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다