爲什麼這段代碼不起作用? 我只想爲道具設置這個類的狀態,但狀態一直是空的。我無法爲道具設置狀態值
class BillsList extends React.Component{
constructor (props: any){
super(props);
this.state = {currentList : this.props.list};
};
render(){
console.log(this.props.list); //It worked..!
console.log(this.state.currentList); //But this is empty
return(
<div className="bill_list">
{this.state.currentList.map((item,i)=>
<BillsItem key ={i} value={item} />
)}
</div>
)
}
};
'名單' 是這樣的 enter image description here
感謝您的想法之下,但它不工作,要麼 –
它爲我工作。無論如何,你可以閱讀這個更多的信息:https://medium.com/@justintulk/react-anti-patterns-props-in-initial-state-28687846cc2e –