下午好!反應變量在jsx中變化
我一直在跟隨教程玩過,並遇到了一個我似乎無法理解的問題。
每當我傳遞變量通過this.props.updateSurveyText(this.props.index, newText)
他們改回舊的變量,當他們進入updateSurveyText={this.updateSurvey.bind(i, newText[0], newText[1])}
記錄顯示在第一功能正確的變量,但是當我在第二個功能記錄他們,他們是不變的。
非常感謝幫助!
調查:
import React from 'react';
import ReactDOM from 'react-dom';
export default class Survey extends React.Component{
constructor(props){
super(props);
this.state = {
editing : false,
title : "",
desc : "",
};
this.edit = this.edit.bind(this);
this.save = this.save.bind(this);
this.remove = this.remove.bind(this);
}
edit() {
this.setState({editing: true});
}
remove() {
console.log('delete');
this.props.deleteFromBoard(this.props.index)
}
save() {
var title = this.refs.newTitle.value;
var desc = this.refs.newDesc.value;
var newText = [title, desc];
console.log(newText[0]);
this.props.updateSurveyText(this.props.index, newText);
this.setState({editing: false});
}
renderNormal(){
return(
<div className="surveyContainer">
<div className="surveyTitle">{this.props.children[0]}</div>
<div className="surveyDesc">{this.props.children[1]}</div>
<button onClick={this.edit} className="button-primary">Edit</button>
<button onClick={this.remove} className="button-primary">Remove</button>
</div>
);
}
renderForm(){
return(
<div className="surveyContainer">
<textarea ref="newTitle" defaultValue={this.props.children[0]}></textarea>
<textarea ref="newDesc" defaultValue={this.props.children[1]}></textarea>
<button onClick={this.save} className="button-primary">Save</button>
</div>
);
}
render(){
if(this.state.editing){
return this.renderForm();
}else{
return this.renderNormal();
}
}
}
局:
import Survey from './Survey.jsx';
import React from 'react';
import ReactDOM from 'react-dom';
export default class Board extends React.Component{
constructor(props){
super(props);
this.state = {
surveys: [
],
};
this.updateSurvey = this.updateSurvey.bind(this);
this.removeSurvey = this.removeSurvey.bind(this);
this.eachSurvey = this.eachSurvey.bind(this);
this.addSurvey = this.addSurvey.bind(this);
}
addSurvey(title, desc){
var arr = this.state.surveys;
arr.push([title, desc]);
this.setState({surveys: arr})
}
removeSurvey(i){
var arr = this.state.surveys;
arr.splice(i, 1);
this.setState({surveys: arr})
}
updateSurvey(newTitle, newDesc, i){
console.log(newTitle);console.log(newDesc);
var arra = this.state.surveys;
arra[i] = [newTitle, newDesc];
this.setState({surveys: arra});
}
eachSurvey(newText, i){
return(
<Survey key={i} index={i} updateSurveyText={this.updateSurvey.bind(i, newText[0], newText[1])} deleteFromBoard={this.removeSurvey.bind(i)}>
{newText[0]}
{newText[1]}
</Survey>);
}
render(){
return(
<div>
<button onClick={this.addSurvey.bind(null, "Titel", "Desc")}>Add new</button>
<div className="board">
{this.state.surveys.map(this.eachSurvey)}
</div>
</div>
)
}
}
感謝在清除了,那藏漢解釋了一些奇怪的行爲由於結合。但是我不太確定如何更新狀態而無需綁定或如何使用不同的綁定。你能否提供一個提示來解決這個問題? – Conroyd
當您將其傳遞給'updateSurveyText'時,不要將任何變量綁定到'updateSurvey'。您的調查組件已經有道具中的索引,文本值將從您的參考中抓取。只需傳遞'updateSurveyText = {this.updateSurvey}'並在'save'函數中用正確的參數'this.props.updateSurveyText(newTitle,newDesc,this.props.index)調用它;''''''''''''''將文本片段包裝成一個數組。確保你傳遞'this.props.index'作爲第三個參數。你的例子有錯誤的觀點。 – jbielick
非常感謝!然而,正如你在我的updateSurvey函數中看到的那樣,我會像這樣調用狀態:var arra = this.state.surveys;這會導致類型錯誤:this.state未定義。我是否應該將狀態轉換爲功能? – Conroyd