Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about the second value in fromPromise function (optional value) #280

Open
tzipisheinin opened this issue Oct 15, 2020 · 3 comments

Comments

@tzipisheinin
Copy link

I'm sending the same promise that I'm fetching the data into (exactly as the example).
I excepted to not see the pending state at all (some loading I have on pending state). but I still can see it - passing very quick, but exist.
Why?

@tzipisheinin
Copy link
Author

Can someone please help with that?

@mweststrate
Copy link
Member

Building a small code sandbox demonstrating your problem typically helps

@tzipisheinin
Copy link
Author

You absolutely right. Thanks for answering.
I made an example now, but the strange thing is that in this small example it's stucked on the pending level every time I send the optional value to the fromPromise function. (if I send only request it does work)

Could be I'm using old version?
My mobx versions are :

"mobx": "^5.0.2",
"mobx-react": "^5.2.3",
"mobx-utils": "^5.1.0",

The store

class Results {

   @observable searchLoader;

   constructor() {
      this.search();
   }

   @action
   search = () => {
      this.searchLoader = fromPromise(
         window.fetch("https://audyx-alpha.s3-eu-west-1.amazonaws.com/fond.jpg"),
         this.searchLoader
      )
   }

   @computed
   get imageSrc() {
      if (this.searchLoader && this.searchLoader.value) {
         return this.searchLoader.value.url;
      }
   }
}

const result = new Results();
export default result;

The Components

@inject('appStore')
@observer
class Image extends React.Component {

   constructor(props) {
      super(props);
      props.appStore.example.search();
   }

   render() {
      const { imageSrc, searchLoader } = this.props.appStore.example;
      return (
         <AsyncRender promise={searchLoader}>
            <img alt="audyx" src={imageSrc}></img>
         </AsyncRender>
      )
   }
}

const Example = () => {
   const [showData, setShowData] = useState(false);

   return (
      <React.Fragment>
         <button onClick={() => setShowData(!showData)}>click to toggle</button>
         {showData ?
            <Image /> : <div>Nothing!</div>}
      </React.Fragment>
   )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants