亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 reactjs 狀態從數組中的項目訪問數據?

如何使用 reactjs 狀態從數組中的項目訪問數據?

眼眸繁星 2021-12-12 10:48:47
我正在嘗試從我從 Django-rest API 獲取的數組中的主對象中獲取數據 (id)。我將使用 id 將它傳遞到一個 fetch url。我嘗試使用 item.id 但它沒有得到 id 值。這是“任務”數組的格式。[    {        "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`      }    })我需要訪問任務的 ID。(在這種情況下,“id”:4)
查看完整描述

1 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

您需要在回調中獲取 idsetState以確保它已被設置:


 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 響應中傳遞 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))

 }


查看完整回答
反對 回復 2021-12-12
  • 1 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號