4963

Make single MobX autorun or reaction for observable property of all objects in array

Question:

I have class with @observable (all examples are Typescript/pseudocode)

class Page { id: number = 0; @observable isVisible: boolean = false; } let array = [new Page(), new Page(), new Page()];

And some functions like:

changeVisibility(obj) { //ajax call like .post("/api/changeVisibility/", {id:obj.id, isVisible:obj.isVisible}) }

And I want to react on isVisible change on any object.

I can enumerate array and make something like:

array.forEach(el => { reaction( () => el.isVisible, isVis => changeVisibility(el); }); });

But can I do that with one function?<br /> Kind of "array observer that reacts to element's property change".

Something like this:

reaction(array, //source (el) => el.isVisible, //observable to react (el) => changeVisibility(el) //callback with object )

Answer1:

If the reaction is responsible for sending the the update of an individual page, I would setup that reaction in the Page constructor itself or have a utility function in Page for that, so that you don't have to keep your pages array in sync with your reaction disposers array (but as best practice, do dispose the reaction if you want to delete the page)

Recommend

  • Reactjs require not defined
  • Realm React-Native: Access same realm from JS(react native code) and android(java)
  • Is it possible to access raw iphone audio output?
  • How to create local push notification using react-native-push-notification
  • Router.create in react-router v1.0.0
  • Matrix problem Python
  • Grouping vars in function
  • Why does Internet Explorer sometimes trigger a local storage event before the data's ready? [du
  • How can i use same custom fonts for both android and ios using react native
  • Javascript syntax null : ?{}
  • React Native fetch is not a function
  • Click button with javascript
  • Webpack-dev-server and isomorphic react-node application
  • Print: Entry, “:CFBundleIdentifier”, Does Not Exist have tried most solutions
  • what is the purpose of “export as namespace foo”?
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • How can we prepend rows to a react native list-view?
  • XSLT foreach repeating nodes to flat
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • List images(01.png) and descriptions(01.txt) from directory
  • Copy to all folders batch file?
  • Using a canvas object in a thread to do simple animations - Java
  • Adjust width of select element according to selected option's width
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • How to attach a node.js readable stream to a Sendgrid email?
  • D3 nodes and links from JSON with nested arrays of children
  • Django rest serializer Breaks when data exists
  • Hazelcast - OperationTimeoutException
  • RestKit - RKRequestDelegate does not exist
  • Revoking OAuth Access Token Results in 404 Not Found
  • Free memory of cv::Mat loaded using FileStorage API
  • Memory offsets in inline assembly
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • Binding checkboxes to object values in AngularJs
  • Why do underscore prefixed variables exist?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize