Die*_*nho 6 reactjs react-lifecycle react-props
我正在尝试将道具从父组件传递到子组件,即使它被调用了两次(不知道为什么,componentDidMount应该只被调用一次),道具似乎还是空的。
父组件:
class Members extends Component{
constructor(props){
super(props);
this.state = {
interests: []
}
}
componentDidMount(){
fetch(interestUrl, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": this.props.authToken
}
})
.then((response) => response.json())
.then((json) => {this.setState({interests: json})})
.catch(error => {console.log("Error: " + error)})
};
render(){
return(
<div className="Members-body">
<div className="Menu-sidebar">
<Menu interestList = {this.state.interests}/>
</div>
<div className="Main-container">
<Main/>
</div>
</div>
)
}
}
export default Members;
Run Code Online (Sandbox Code Playgroud)
子组件:
class Menu extends Component {
constructor(props){
super(props);
}
componentDidMount(){
console.log("interestList: " + this.props.interestList);
}
render() {
return(
<div className="Menu-container">
este es el menu de la aplicacion
</div>
)
}
}
export default Menu;
Run Code Online (Sandbox Code Playgroud)
来自Menu组件内componentDidMount的控制台日志打印:
interestList:
interestList:
Run Code Online (Sandbox Code Playgroud)
有什么想法可以指出正确的方向吗?不胜感激!
| 归档时间: |
|
| 查看次数: |
4651 次 |
| 最近记录: |