Passing promise output as a prop using MobX: A Quick Guide

In my App.js file I have the following code snippet:

  import stores from 'client/stores';
  ...
  ...
  render() {
     return (
        <Provider {...stores}>
          <SafeAreaView>
            <AppContainer />
          </SafeAreaView>
       </Provider>
     );
  }

To fetch data from the backend and pass it to AppContainer asynchronously using a promise, here is an example:

// client/stores/index.js

boardsService.retrieveBoards().then(boards => {
    // Code to store retrieved boards 
})

You can then inject the boards into AppContainer:

export default
@inject('boards')
@observer
class AppContainer extends React.Component {
  constructor(props) {
     super(props);
     console.log(props.boards); 
  }

  render() {
     ...
  }
}

I attempted to do this in stores/index.js as well:

async function connect() {
  const connection = await boardsService.retrieveBoards();
  if (connection) {
    return connection;
  }
  return null;
}

connect().then(boards => {
  exports.boards = boards;
}); 

However, I encountered the following error message:

https://i.sstatic.net/Ojf8Y.png

Answer №1

Initially, Mobx Actions alter observable properties without returning a value, whether they are synchronous or asynchronous.

@observable boards = [];

boardsService.retrieveBoards().then(boards => {
 this.boards.replace(boards);
})

Additionally, it is advised to dereference the observable property within the render function rather than in the constructor.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Need help with fixing the problem in my side menu bar and making the content scroll smoothly

Check out my code on JS Fiddle here: http://jsfiddle.net/kodi/d2s3uja0/1/ I'm having trouble getting my side menu bar fixed and the content to scroll. I want to display the menu vertically on the left just like this: M E N U Any suggestions on how ...

Is it possible to arrange a jQuery list in alphabetical order that is being dynamically generated from a JSON file?

I am in the process of developing an application featuring a jQuery mobile directory that loads three sections of names from a JSON file. My main concern is whether there is a method to automatically arrange the items from the JSON array in alphabetical or ...

Change the Bootstrap components according to the size of the screen

Is there a built-in Bootstrap feature to change an element's class based on screen size? I'm working on a webpage with scrollable card elements. On desktop, the cards are arranged horizontally, but on mobile they are stacked vertically, requirin ...

Maximizing Efficiency: Sending Multiple Responses during computation with Express.js

Seeking a way to send multiple responses to a client while computing. See the example below: app.get("/test", (req, res) => { console.log('test'); setTimeout(() => { res.write('Yep'); setTime ...

Issue with Bootstrap's form-inline element causing incorrect spacing in btn-group

I'm having an issue with Bootstrap components. When I try to input numbers, everything works fine as long as I don't use form-inline or a navbar. Check out my fiddle here: http://fiddle.jshell.net/6Lrhcezb/ https://i.sstatic.net/hzmlM.png The ...

Looking to scan through a directory of .html files in Node.js to find specific element attributes?

Imagine trying to tackle this task - it's like reaching for a needle in a haystack. Picture a folder containing a static website, complete with images, stylesheets, and HTML files. My Node application needs to dive into this folder and extract only th ...

What are the steps to incorporate SVG into a React Native project?

I'm in the process of integrating bootstrap icons into my react native project, but I've been having trouble finding clear instructions on how to render an SVG in react-native. Can anyone provide some guidance on this? ...

What steps can be taken to enhance the efficiency of this complex nested asynchronous loop?

The issue at hand involves an array of objects structured like this: let myObj = [ {'db1':['doc1','doc2','doc3']}, {'db2':['doc4','doc5']}, {'db3':['doc7','doc8 ...

The optimal and most secure location for storing and retrieving user access credentials

After receiving a list of locations accessible to the session user from the server, I am seeking the ideal location to store these roles in Angular. This will allow me to determine whether or not to display specific routes or buttons for the user. Where ...

Error in socket connection when making multiple GET requests within a forEach loop

Struggling to fetch multiple images from different URLs using forEach and fetch(url) simultaneously. Additionally, attempting to save them to disk by piping them to a stream is causing issues. The request count significantly slows down after around 900 req ...

The required `React-Core` specification for `RNCPicker` version 1.6.0 with React Native 0.59 could not be located

Encountered an issue when attempting to run pod install for RNCPicker version 1.6.0 with React Native 0.59. Analyzing dependencies [!] Unable to find a specification for `React-Core` relied upon by `RNCPicker` Possible reasons: * outdated source reposito ...

Use jQuery to perform an action when an input is detected

Hello and thank you for taking the time to read this message. I have set up an input box where users can enter URLs. Once a user enters a URL, they must click a button in order to retrieve values from it. Here is the code snippet: jQuery(document).ready ...

Trouble arises when dealing with components beyond just the background while using jquery_interactive_bg.js

After experimenting with different plugins, I managed to achieve a parallax effect on my website's landing page by using the interactive_bg.js plugin. Following the steps outlined in a demo tutorial, I was able to attain the desired visual effect. Be ...

Unable to write to file due to permission restrictions from EPERM. Will delete existing file and create a new one. This action

I am encountering an issue with my file system function that involves deleting a file and creating a new one with updated data. The error occurs randomly, not consistently, happening approximately every other time the code runs. Below is my current impleme ...

React-Native error message: Promise rejection unhandled - React child cannot be an object

I am experiencing an issue with rendering a list from an array of objects that have the structure provided below. While I have successfully handled the promise and set the data to the state, I keep encountering an error specifically with this array. The da ...

How to use the transform function in JavaScript

Hey there, I'm looking to create a new function. I am just starting out with javascript. I want to wrap the code below in a function like: ConfirmDelete() { }; This function should perform the same action as this code snippet: $('.btn-danger ...

An example of ngRoute demonstrating the functionality of multiple controllers

I am currently working on a small ngRoute example to incorporate multiple applications and controllers. The first app/controller is for the main page, while the second set of app/controllers is for the HTML that ngRoute loads after pressing a button. Howev ...

Tips on attaching the main element of a partial view

I have a scenario in my ASP.NET MVC project where I need to render a partial view once in the main view, and then upon clicking the add button, I want to append the entire partial view to the form. However, when I click add, only the child element is added ...

Difficulty encountered in closing div by clicking the background with the help of jquery

I am facing a challenge with properly closing a div container and restoring it to its original state when I click outside of it. Despite trying various solutions from stackoverflow and extensive internet research, I have been unable to find or come up with ...

Use ngResource's $delete method to remove a record from a query object

Just starting out with angular and trying to work with the $resource library for API services. I'm having trouble figuring out how to delete a record obtained through the query() method. Specifically, we have an endpoint for user notifications. The go ...