Javascript 在这种情况下,setState如何不起作用?
我正在发出post请求,并根据收到的响应设置对象的状态。我需要使用的数据,以便我可以映射它并在下拉列表中显示它 尝试使用Javascript 在这种情况下,setState如何不起作用?,javascript,reactjs,Javascript,Reactjs,我正在发出post请求,并根据收到的响应设置对象的状态。我需要使用的数据,以便我可以映射它并在下拉列表中显示它 尝试使用长度,但无效 组件中的状态: this.state = { assignees: [] }; axios .post("http://localhost:3600/get_assignees", { id: id }) .then(res => { this.setState({ assginees: res.data }); }); cons
长度
,但无效
组件中的状态:
this.state = {
assignees: []
};
axios
.post("http://localhost:3600/get_assignees", {
id: id
})
.then(res => {
this.setState({ assginees: res.data });
});
const { assginees } = this.state;
API请求:
this.state = {
assignees: []
};
axios
.post("http://localhost:3600/get_assignees", {
id: id
})
.then(res => {
this.setState({ assginees: res.data });
});
const { assginees } = this.state;
解构:
this.state = {
assignees: []
};
axios
.post("http://localhost:3600/get_assignees", {
id: id
})
.then(res => {
this.setState({ assginees: res.data });
});
const { assginees } = this.state;
映射到select元素时抛出无法读取未定义的属性长度
<select className="Dropdown"
name="assginees"
defaultValue={"DEFAULT"} > {
assginees.length
? assginees.map(item => (
<option value={item} key={item}>
{item.login}
</option>
))
: null }
</select>
{
助理长度
?相关图纸(项目=>(
{item.login}
))
:null}
响应结构与此处接收到的结构类似
GET/repos/:owner/:repo/collaborators
这是为了获得可用的受让人数量
接收数据时应设置状态,我应能够在选择选项中映射数据。问题就在这里
const { assginees } = this.state;
您在这里有状态被指派者
,而不是助理
输入错误
就换成这个,
const { assignees } = this.state;
{assignees.length>0
?受让人。地图(项目=>(
{item.login}
))
:null}
问题就在这里
const { assginees } = this.state;
您在这里有状态被指派者
,而不是助理
输入错误
就换成这个,
const { assignees } = this.state;
{assignees.length>0
?受让人。地图(项目=>(
{item.login}
))
:null}
您介意显示整个组件吗?不清楚何时调用每个代码段,何时指定初始状态?在构造函数中或在某些生命周期方法中?初始状态是assignees
,而后面的所有引用都是assginees
Yikes,拼写错误,您介意显示整个组件吗?不清楚何时调用每个代码段,何时指定初始状态?在构造函数中或在某些生命周期方法中?初始状态为assignees
,而后面的所有引用都是assgines
Yikes,拼写错误