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

Add support for returning a promise from a data provider function #314

Open
acqajar opened this issue Mar 18, 2019 · 5 comments · May be fixed by #611
Open

Add support for returning a promise from a data provider function #314

acqajar opened this issue Mar 18, 2019 · 5 comments · May be fixed by #611

Comments

@acqajar
Copy link

acqajar commented Mar 18, 2019

Steps to reproduce:

  1. Here's my code below:
    <MentionsInput singlieLine={false} value={description.value} onChange ={this.handleChangeStoryDescription}> <Mention trigger="@" data={ this.fetchUsers } /> </MentionsInput>
  2. in the fetchUsers call, I have the following:
    fetchUsers = (query)=> { let users; console.log(query) if (!query) return axios.get(getUsers(query)).then( ({ data }) => { users = data.data.map((e, idx)=>{ let obj = { id: e._id, display: e.firstName + " " + e.lastName, } return obj }) return users // users is an array of objects formatted as per above }) .catch( ( error ) => { console.log( error ); }).then(function (users) { console.log(users) return users }); }
  3. My expectation is that the return of users to the this.fetchUsers function in the works as it does in this example - https://github.com/signavio/react-mentions/blob/master/demo/src/examples/AsyncGithubUserMentions.js. In other words, the data attribute in the should populate with the array of objects i.e. users
@jfschwarz
Copy link
Contributor

jfschwarz commented Mar 18, 2019

We currently don't support promises as return values but only good old callbacks. You can make it work by appending .then(callback) like you also see it in the linked example.

It would be nice to enhance react-mentions adding support for returning promises, though. :)

@jfschwarz jfschwarz changed the title Async Issue Add support for returning a promise from a data provider function Mar 18, 2019
@acqajar
Copy link
Author

acqajar commented Mar 18, 2019

We currently don't support promises as return values but on the good old callback. You can make it work by appending .then(callback) like you also see it in the linked example.

It would be nice to enhance react-mentions adding support for promises, though. :)

Hm. I tried that as well -
` fetchUsers = (query)=> {
let users;
console.log(query)
if (!query) return
axios.get(url.findUser(query)).then(resp =>
resp.data.data.map(user => ({ display: user.firstName, id: user._id }))
)

}`

The issue still persists (the above is identical to the example other than using axios instead of fetch, which should be inconsequential).

@jfschwarz
Copy link
Contributor

Your example still does not call the callback (second arg provided to your function). Change your code to:

fetchUsers = (query, callback)=> {
  let users;
  console.log(query)
  if (!query) return
  axios.get(url.findUser(query)).then(resp =>
    resp.data.data.map(user => ({ display: user.firstName, id: user._id }))
  ).then(callback)
}

@acqajar
Copy link
Author

acqajar commented Mar 18, 2019

Wow. Yes. When I changed it away from the promise I forgot to add in the callback. Apologies and many thanks for the quick response! Thank you. :)

@acqajar
Copy link
Author

acqajar commented Mar 18, 2019

Your example still does not call the callback (second arg provided to your function). Change your code to:

fetchUsers = (query, callback)=> {
  let users;
  console.log(query)
  if (!query) return
  axios.get(url.findUser(query)).then(resp =>
    resp.data.data.map(user => ({ display: user.firstName, id: user._id }))
  ).then(callback)
}

Also - is there any way to grab the values of the object in the array that is selected from the dropdown? I see onAdd but it seems like it only passes back the "id" back, and I need to grab several values.

@srmagura srmagura linked a pull request Jul 22, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants