简体   繁体   English

mobx -state-tree 中的代理

[英]Proxy in mobx -state-tree

I'm trying to build a simple budgeting app.我正在尝试构建一个简单的预算应用程序。 Whenever I insert this model into my app.每当我将此 model 插入我的应用程序时。 I get a proxy for the expenses.我得到了费用的代理。 Where is the flaw in my thinking?我的思维缺陷在哪里?

I have an action on the Budget.js when I print it in the useEffect this is what console.log outputs for the expenses a proxy.当我在 useEffect 中打印 Budget.js 时,我对 Budget.js 进行了操作,这是 console.log 为代理费用输出的内容。 I'm expecting it to print the actual data from the initial state.我期待它从最初的 state 打印实际数据。


React.useEffect(() => {
    budget.addDummyData()
    console.log(budget.expenses)
  }, [])


[[Handler]]: Object
[[Target]]: Array(0)
[[IsRevoked]]: false


//////////////////////////////////////////////////////////////////////
//SubCategory
const SubCategory = types
  .model('SubCategory', {
    id: types.maybeNull(types.string, ''),
    name: types.maybeNull(types.string, ''),
    amount: types.maybeNull(types.number, 0)
  })
const SubCategoryStore = types.model({ subCategory: types.optional(SubCategory, {}) })
export default SubCategoryStore
/////////////////////////////////////////////////////////////////////////
//Category.js
const Category = types
  .model('Category', {
    id: types.maybeNull(types.string, ''),
    name: types.maybeNull(types.string, ''),
    subCategories: types.array(SubCategory)
  })
const CategoryStore = types.model({ category: types.optional(Category, {}) })
export default CategoryStore
///////////////////////////////////////////////////////////////
// Budget
const Budget = types
  .model('Budget', {
    totalIncome: 200,
    expenses: types.array(Category)
    // incomes: types.optional(types.array(Category), [])
  }).actions({
    addDummyData() {
      self.expenses.push(initialStateExpenses)
    }
})
const BudgetStore = types.model({ budget: types.optional(Budget, {}) })
export default BudgetStore


const initialStateExpenses = {
  id: '123',
  name: 'Food',
  subCategories: [
    {
      id: '1314',
      name: 'Grocery',
      amount: 250
    },
    {
      id: '1442',
      name: 'Restaurants',
      amount: 50
    }
  ]
}

expenses is of type Category[], you are passing an object.费用属于类别 [],您传递的是 object。 I assume you want to set the expenses from subCategories .我假设您想从subCategories设置费用。 If so you can try this如果是这样你可以试试这个

    addDummyData() {
      initialStateExpenses.subCategories.forEach(ex => self.expenses.push(ex))
    }
or
    addDummyData() {
      self.expenses = initialStateExpenses.subCategories
    }

A better approach would be to pass the initialStateExpenses via args to the addDummyData function so your model doesn't depend on external variables更好的方法是通过 args 将initialStateExpenses传递给addDummyData function 所以你的 model 不依赖于外部变量

    addDummyData(initialStateExpenses) {
      initialStateExpenses.subCategories.forEach(ex => self.expenses.push(ex))
    }
or
    addDummyData(initialStateExpenses) {
      self.expenses = initialStateExpenses.subCategories
    }

then use it like
    budget.addDummyData(initialStateExpenses)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM