第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 reactjs 狀態(tài)從數(shù)組中的項(xiàng)目訪問數(shù)據(jù)?

如何使用 reactjs 狀態(tài)從數(shù)組中的項(xiàng)目訪問數(shù)據(jù)?

眼眸繁星 2021-12-12 10:48:47
我正在嘗試從我從 Django-rest API 獲取的數(shù)組中的主對象中獲取數(shù)據(jù) (id)。我將使用 id 將它傳遞到一個(gè) fetch url。我嘗試使用 item.id 但它沒有得到 id 值。這是“任務(wù)”數(shù)組的格式。[    {        "title": "Task 4",        "description": "Task 4",        "video": "ad12312312",        "done": false,        "steps": [            {                "title": "Task 4 Task 1",                "description": "Task 4 Task 1",                "done": false,                "task_id": 4,                "id": 10            },            {                "title": "Task 4 Task 2",                "description": "Task 4 Task 2",                "done": false,                "task_id": 4,                "id": 11            },            {                "title": "Task 4 Task 3",                "description": "Task 4 Task 3",                "done": false,                "task_id": 4,                "id": 12            }        ],        "id": 4    }]class Screen extends Component {  constructor() {    super();    this.state = {      task: [],    };  }  componentDidMount() {    AppState.addEventListener('change', this._handleAppStateChange);    fetch('https://url.com/daily-ecommerce/', {      method: 'GET',      headers: {        'Authorization': `Token xxxxx`      }    })    .then( res => res.json())    .then( jsonRes => this.setState({ task: jsonRes }) )    .catch( error => console.log(error))    .then(console.log(this.state.task.id))    fetch(`https://url.com/step/?task_id=${this.state.task.id}`, {      method: 'GET',      headers: {        'Authorization': `Token xxxxx`      }    })我需要訪問任務(wù)的 ID。(在這種情況下,“id”:4)
查看完整描述

1 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

您需要在回調(diào)中獲取 idsetState以確保它已被設(shè)置:


 componentDidMount() {

   AppState.addEventListener('change', this._handleAppStateChange);

   fetch('https://url.com/daily-ecommerce/', {

       method: 'GET',

       headers: {

         'Authorization': `Token xxxxx`

       }

     })

     .then(res => res.json())

     .then(jsonRes => this.setState({

       task: jsonRes

     }, () => {

       fetch(`https://url.com/step/?task_id=${this.state.task[0].id}`, {

         method: 'GET',

         headers: {

           'Authorization': `Token xxxxx`

         }

       })

     }))

     .catch(error => console.log(error))

 }

另一種方法是直接從 json 響應(yīng)中傳遞 id:


  componentDidMount() {

   AppState.addEventListener('change', this._handleAppStateChange);

   fetch('https://url.com/daily-ecommerce/', {

       method: 'GET',

       headers: {

         'Authorization': `Token xxxxx`

       }

     })

     .then(res => res.json())

     .then(jsonRes => {

       this.setState({

         task: jsonRes

       })

       fetch(`https://url.com/step/?task_id=${jsonRes[0].id}`, {

         method: 'GET',

         headers: {

           'Authorization': `Token xxxxx`

         }

       })

     })

     .catch(error => console.log(error))

 }


查看完整回答
反對 回復(fù) 2021-12-12
  • 1 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號