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

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

如何管理反應中的狀態?

如何管理反應中的狀態?

慕容3067478 2021-08-20 19:03:44
我正在實現一個顯示任務和項目的反應應用程序,但我不知道如何管理狀態import React from 'react';import './App.css';import Header from './Components/Header.js'import Projects from './Components/ProjectsList'import Task from './Components/Task'import Grid from '@material-ui/core/Grid';import Typography from '@material-ui/core/Typography';import Hours from './Components/Hours'var projects = [  {    "id": 1,    "name": "Fifa19",    'tasks': [      { id: "1", name: 'dise?ar', "progress": 10 },      { id: "2", name: 'trabajar', "progress": 20 },      { id: "3", name: 'construir', "progress": 30 },      { id: "4", name: 'pensar', "progress": 40 },      { id: "5", name: 'rehacer', "progress": 50 },    ]  },  {    "id": 2,    "name": "StarWars",    'tasks': [      { id: "5", name: 'dise?ar', "progress": 10 },      { id: "6", name: 'trabajar', "progress": 20 },      { id: "7", name: 'construir', "progress": 30 },      { id: "8", name: 'pensar', "progress": 40 },      { id: "9", name: 'rehacer', "progress": 50 },    ]  },  {    "id": 3,    "name": "Pocoyo",    'tasks': [      { id: "10", name: 'dise?ar', "progress": 10 },      { id: "11", name: 'trabajar', "progress": 20 },      { id: "12", name: 'construir', "progress": 30 },      { id: "13", name: 'pensar', "progress": 40 },      { id: "14", name: 'rehacer', "progress": 50 },    ]  },  {    "id": 4,    "name": "Doraemon",    'tasks': [      { id: "15", name: 'dise?ar', "progress": 10 },      { id: "16", name: 'trabajar', "progress": 20 },      { id: "17", name: 'construir', "progress": 30 },      { id: "18", name: 'pensar', "progress": 40 },      { id: "19", name: 'rehacer', "progress": 50 },    ]我正在實施一個反應應用程序,它顯示了任務和項目。一個項目可以有很多任務。我的主要視圖是所有可用的項目,當單擊其中一個時,您應該看到它們對應的任務。我的問題是:有了好的做法,我應該如何處理應用程序的狀態?是否應該顯示任務?我現在的申請是這樣的:我有一個帶有項目組件和其他任務的應用程序組件。每一個都分別有幾個項目和任務組件。
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

您應該設置選定的項目 ID,而不是在狀態中設置任務列表。為任務列表創建一個單獨的組件,然后將所選項目的任務列表作為道具傳入。


查看完整回答
反對 回復 2021-08-20
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

這取決于您的設計。您可以在同一屏幕上顯示項目的任務,或者您可以有一個單獨的頁面來這樣做。此外,由于您沒有處理這些數據的后端服務,因此在如何處理狀態方面沒有太多可供選擇的選項。


如果要在同一頁面中顯示任務,則必須將狀態修改為與項目變量非常相似:


state={

 {

    "id": 1,

    "name": "Fifa19",

    'tasks': [

      { id: "1", name: 'dise?ar', "progress": 10 },

      { id: "2", name: 'trabajar', "progress": 20 },

      { id: "3", name: 'construir', "progress": 30 },

      { id: "4", name: 'pensar', "progress": 40 },

      { id: "5", name: 'rehacer', "progress": 50 },

    ]

  },

  {

    "id": 2,

    "name": "StarWars",

    'tasks': [

      { id: "5", name: 'dise?ar', "progress": 10 },

      { id: "6", name: 'trabajar', "progress": 20 },

      { id: "7", name: 'construir', "progress": 30 },

      { id: "8", name: 'pensar', "progress": 40 },

      { id: "9", name: 'rehacer', "progress": 50 },

    ]

  },

  {

    "id": 3,

    "name": "Pocoyo",

    'tasks': [

      { id: "10", name: 'dise?ar', "progress": 10 },

      { id: "11", name: 'trabajar', "progress": 20 },

      { id: "12", name: 'construir', "progress": 30 },

      { id: "13", name: 'pensar', "progress": 40 },

      { id: "14", name: 'rehacer', "progress": 50 },

    ]

  },

  {

    "id": 4,

    "name": "Doraemon",

    'tasks': [

      { id: "15", name: 'dise?ar', "progress": 10 },

      { id: "16", name: 'trabajar', "progress": 20 },

      { id: "17", name: 'construir', "progress": 30 },

      { id: "18", name: 'pensar', "progress": 40 },

      { id: "19", name: 'rehacer', "progress": 50 },

    ]

  },

  {

    "id": 5,

    "name": "Lineage 3",

    'tasks': [

      { id: "20", name: 'dise?ar', "progress": 10 },

      { id: "21", name: 'trabajar', "progress": 20 },

      { id: "22", name: 'construir', "progress": 30 },

      { id: "23", name: 'pensar', "progress": 40 },

      { id: "24", name: 'rehacer', "progress": 50 },

    ]

  }

}

或者,如果您希望您的應用程序單獨處理它,則需要使用 react-router 將路由映射到其他組件并傳遞 props 以顯示您想要的數據。類似的東西:


<Route

  path='/tasks'

  render={(props) => <Tasks{...props}/>}

/>

在這種情況下,任務將是一個功能性/啞組件,因為它僅用于顯示數據,并且只有在用戶單擊項目或您可能想要觸發的任何事件時才會顯示。


查看完整回答
反對 回復 2021-08-20
  • 2 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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