Rename the keys of an imported array on a single line (much like destructuring)

Viewed 2274

In javascript and React Native, I use an API function which returns two elements: results and error.

import { findAll as forumFindAll } from '../../Api/ForumApi'

const IndexScreen = () => {
    const [results, error] = forumFindAll()
    ...

I would like to rename the variables results and error on a single line (much like destructuring).

Something like that (but I know it doesn't work) :

const [results as forumsResults, error as forumsError] = forumFindAll()
1 Answers

Since forumFindAll returns an array, there should be no need to "rename" any of its items - simply put in the names you want into the destructuring syntax:

const [forumsResults, forumsError] = forumFindAll();

The return value of forumFindAll does not have any intrinsic connection to the results and error names, you can choose whatever names you wish.

Related