import React, {Component} from 'react';
import { Link } from 'react-router';
class AsideNav extends Component{
constructor(props, context) {
super(props, context);
this.state = {
navigation: ''
};
}
componentWillMount(){
console.log(this.props.data.navigation); // data from parent received
this.setState({
navigation: this.props.data.navigation.map(function(el, index){
return(
<Link key={index} className={"aside-nav__link " + (el.modifier ? ("aside-nav__link" + el.modifier) : '')} to={el.url}>{el.name}</Link>
)
})
})
}
render() {
console.log(this.state.navigation); // JSX for navigation is ready
return (
<nav className="aside-nav">
<span className="aside-nav__title" >Категории</span>
{this.state.navigation}
</nav>
);
}
}
export default AsideNav
後(貌似)成功執行的代碼,它會顯示我一個空塊和反應兩次安裝組件,但第二次沒有收到道具
Uncaught TypeError: Cannot read property 'navigation' of undefined at AsideNav.componentWillMount...
使用componentWillReceiveProps(newProps)第二次獲取道具作爲構造函數和componentDidMount方法只執行一次。 – Ved
其實,你的錯誤指出''undefined'上的'navigation'被調用,這意味着你在'props'中收到的'data'是未定義的。簡而言之,您不會在道具中收到「數據」。 – Panther
你能解釋兩次渲染是什麼意思嗎? –