我的previous question中的問題最近已解決,現在已創建一個新問題,其中我的數組中的值未顯示在呈現的下拉列表中。使用我在上一個問題中給出的答案,用正確的鍵填充列表,但我對數組中的值感興趣,而不是鍵值。我在下面列出了我的修改後的渲染方法,以及我的應用程序中另一個組件的類原型接收到的類數據類型的示例。React.JS - 未在動態呈現列表中顯示的值
編輯: 我找到了this source這正是我想要做的事情,但是在嘗試他們的代碼時,我在控制檯中得到一個錯誤,指出「e.map不是函數」。我發佈了我在下面試過的代碼。
編輯2:爲了不進行太多的細節,我從組件的初始安裝(組件安裝功能如下)時通過AJAX請求從我的SQL Server數據庫中檢索數據。我通過.Net/C#做我的服務器端的東西,並通過xhttp.responseText和string.prototype.split將數據返回到客戶端。爲了加快調試速度,我一直跳過ajax請求,不要每次都依賴數據庫調用(因此爲什麼要檢查404錯誤),而是將名爲'n'的顯式聲明數組設置爲類的狀態。我的Dropdown組件(有問題的組件)有一個名爲「classes」的屬性,它與當前的類的狀態(如上所述設置)相關,該代碼也在下面發佈。
類例如:
['justin','tom','bob']
Render方法:
render: function(){
return(
<div>
<select onChange = {this.change}>
{
Object.keys(this.props.classes).map((value, key) =>{
return <option key = {key}>{value}</option>
}
)}
</select>
</div>
)
}
});
新代碼的嘗試:
export default React.createClass({
getInitialState: function(){
return{classes: this.props.classes }
},
render: function() {
var names = this.state.classes;
console.log(names);
return (
<select>
{names.map(function(name, index){
return <option key={ index }>{name}</option>;
})}
</select>
)
}
});
編輯2代碼:
componentDidMount: function(){
var xhttp;
var myStr;
var user = window.x;
console.log("the current user is "+ user);
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 || xhttp.status == 404) {
document.getElementById("test").innerHTML = xhttp.responseText;
myStr = xhttp.responseText;
//n = myStr.split("\n");
n = ['justin', 'earl', 'samuels', 'tom'];
console.log(n);
this.setState({ classes: n });
}
}.bind(this);
xhttp.open("GET", "/portals/0/js/hello.aspx?q="+user, true);
xhttp.send();
},
下拉組件:
<Dropdown classes = {this.state.classes} />
你能舉一個你的類道具的例子嗎?它的結構如何?假設它是一個名稱陣列,你建議你嘗試的代碼應該沒問題:http://codepen.io/anon/pen/aNxaOM –
https://jsfiddle.net/69z2wepo/42048/你的代碼看起來像是有效的。你可以在你傳遞類數組到你的組件的地方包含代碼嗎? –
我會將其添加一秒 –