復位狀態,我試圖建立在我的網頁中的部分,在那裏有一個2X2的網格顯示四個部分組成。當您單擊其中一個時,該框將展開到屏幕的中心,而其他人則淡出。通過在幾個不同的屬性上調用setState來切換css類,我已經明白了這一點。設置和React.js
什麼我在與當一個「關閉」按鈕被踢以致復位狀態麻煩的箱子承擔其原有的形狀和透明度。我從「handleCloseClick」功能中看到的console.log,所以我知道這是有線的財產。不管如何我遍歷狀態的陣列,我不能改變它們的屬性回到原來的狀態。這是我的代碼。
class Parent extends Component{
constructor(){
super();
this.state =
attrs: {[{
id: 1,
expand: false,
reduced: false,
seeText: false
}],
[{
id: 2,
expand: false,
reduced: false,
seeText: false
}],
[{
id: 3,
expand: false,
reduced: false,
seeText: false
}],
[{
id: 4
expand: false,
reduced: false,
seeText: false
}]}
this.handleClick = this.handleClick.bind(this)
this.handleCloseClick = this.handleClick.bind(this)
}
/*This function works*/
handleClick(e){
const array = this.state.attrs
array.map(function(element){
if(element.id === i){
element.reduced = false;
element.expand = true;
element.seeText = true;
}else{
element.reduced = true;
}
})
this.seState({attrs: array})
}
/*This function will console.log but will not setState of attrs*/
handleCloseClick(){
const newArray = this.state.attrs
newArray.map(function(element(){
element.expand = false;
element.reduced = false;
element.seeText = false;
})
this.setState(attrs: newArray})
}
render(){
const newEls = this.state.attrs;
return(
{newEls.map(function(newEl, index)){
return <Child key={index} onClick={this.handleClick(el)} onCloseClick={this.handleCloseClick()} />
}
)
}
}
請幫幫忙!爲什麼這個該死的狀態不會變回來?!?!
有在'newArray.map(功能的語法錯誤(元件(){ element.expand = FALSE; element.reduced = FALSE; element.seeText = false; })'和應該是'newArray.map(函數(元素){ element.expand = FALSE; element.reduced = FALSE; element.seeText = FALSE; })' –