javascript - MobX state tree - array of models doesn't trigger update - Stack Overflow

I have following model:const AnotherModel = types.model({foo: types.string});export const SomeCollectio

I have following model:

 const AnotherModel = types.model({
    foo: types.string
 });

 export const SomeCollectionModel = types
     .model({
         data: types.array(AnotherModel),
     })
     .views((self) => ({
         getData: () => self.data,
     }))
     .actions((self) => ({
         fetchData: flow(function* fetchData() {
             try {
                 const data =
                     yield service.fetchData();
                 self.data.replace(
                     data.map((f) => AnotherModel.create(f)),
                     // Using plain json instead of create does the same
                 );
             } catch (err) {
                 console.error(err);
             }
         })
    }));

Then I have two React ponents, which I pose with inject and observer.

Parent one (view/page):

pose(
    inject((states) => ({
        fetchData: () =>
            states.myModel.fetchData(),
    })),
    observer,
)

Child one:

pose(
    inject(states => ({
        data: states.myModel.getData()
    })),
    observer
)

I have a re-rednering problem. Initially child ponent doesn't render anything. In the meantime data is fetched (action triggers itself). However data is not updated in child. ponentWillReact doesnt trigger. After changing routes (rerender by router) view gets updated

Do you have any idea? I'm stuck for hours.

I have following model:

 const AnotherModel = types.model({
    foo: types.string
 });

 export const SomeCollectionModel = types
     .model({
         data: types.array(AnotherModel),
     })
     .views((self) => ({
         getData: () => self.data,
     }))
     .actions((self) => ({
         fetchData: flow(function* fetchData() {
             try {
                 const data =
                     yield service.fetchData();
                 self.data.replace(
                     data.map((f) => AnotherModel.create(f)),
                     // Using plain json instead of create does the same
                 );
             } catch (err) {
                 console.error(err);
             }
         })
    }));

Then I have two React ponents, which I pose with inject and observer.

Parent one (view/page):

pose(
    inject((states) => ({
        fetchData: () =>
            states.myModel.fetchData(),
    })),
    observer,
)

Child one:

pose(
    inject(states => ({
        data: states.myModel.getData()
    })),
    observer
)

I have a re-rednering problem. Initially child ponent doesn't render anything. In the meantime data is fetched (action triggers itself). However data is not updated in child. ponentWillReact doesnt trigger. After changing routes (rerender by router) view gets updated

Do you have any idea? I'm stuck for hours.

Share Improve this question asked Oct 25, 2018 at 11:42 Łukasz OstrowskiŁukasz Ostrowski 1,0304 gold badges14 silver badges26 bronze badges 2
  • what versions are you using? i switched to mobx-state-tree from mobx and have had issues described in their docs about nesting observers mobx-react.js/observer-ponent#nesting-caveat – Daniel Lizik Commented May 20, 2020 at 12:34
  • also see this github./mobxjs/mobx-state-tree/issues/1060 – Daniel Lizik Commented May 20, 2020 at 12:38
Add a ment  | 

1 Answer 1

Reset to default 4
  1. You don't need to create a view to access your raw data, you can use the prop directly:
inject(states => ({
  data: states.myModel.data
}))
  1. You don't need to use observer if you use inject. MyComponent will observe what's inside the inject just fine:
inject(states => ({
  data: states.myModel.data
}))(MyComponent)
  1. In this case, you are observing the array, which is a reference. If you update the items of the array, the array itself doesn't change so MyComponent is not re-rendered. You could observe the array length, that way if the length changes, MyComponent is re-rendered, but it's only useful when you add or remove items from the array:
inject(states => ({
  data: states.myModel.data,
  length: states.myModel.data.length
}))(MyComponent)
  1. Finally, if want to observe changes to the items of the array even if the length doesn't change, you need to observe the items internally:
inject(states => ({
  data: states.myModel.data,
  length: states.myModel.data.length
}))(MyComponent)

const MyComponent = ({ data }) => (
  <div>{data.map(item => (
    <MyDataItem item={item} />
  ))}
  </div>
)

inject((states, props) => ({
  foo: props.item.foo
}))(MyDataItem)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644077a4637866.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信