11

I'm writing an application based on react-native and we know definitely we can use JavaScript codes in this project and all react projects.

But when I use below code the transpiler return Error to the simulator:

const mixer = (...arg) => arg.flat(9);

The error is: flat is not function

Why in react-native it is not define but in browser works well?

4 Answers4

5

React Native uses the JavaScriptCore engine which uses the ECMA-262 spec. The spec shows clearly that .flat is not supported and it is not included in the list of transformations for babel.

Write your own or install one of the many shims available on npm.

Wainage
  • 4,892
  • 3
  • 12
  • 22
  • Oh, real thanks to your clarification. I wrote my own based on `reduce` and Array `concat` function. But I wanted to know why It doesn't support, thanks. –  Oct 19 '18 at 13:24
0

instead of arr.flat try using this function

function flatten(arr) {
    return Array.prototype.concat(...arr);
}

flatten(arr)
Atishay Jain
  • 1,425
  • 12
  • 22
  • Actually my exact question is not a function. I said WHY. in other hand your answer is not true, because it just flat first floor of depth. I know how write it recursive and I used `reduce` function of `JavaScript` but I wanna WHY FLAT DOES NOT WORK THERE? –  Oct 18 '18 at 07:23
  • oh in that case, open the link below check the comments https://stackoverflow.com/questions/52283892/transpiling-array-prototype-flat-away-with-babel – Atishay Jain Oct 18 '18 at 07:24
0

Is it supported on your platform? Array.prototype.flat

Link also contains implementations using reduce and concat as well as non-recursive.

JGoodgive
  • 1,068
  • 10
  • 20
  • Please check the OP's post: _Why in browser it works well_ – KarelG Oct 18 '18 at 07:34
  • please check my question, the last sentence, I said why it is not defined in `react-native`? –  Oct 18 '18 at 07:37
  • Well it is a normal array function that is not implemented everywhere. It isnt react native at all it is plain javascript. And I gues your react native is being executed on a platform where it is not supported? – JGoodgive Oct 18 '18 at 07:43
0

const mixer = (...arg) => arg.flat(9);

The error is: flat is not function

Why in react-native it is not define but in browser works well?

My first guess is that the react-native transpiler is not interpreting your ...arg correctly, leading to an object that is not of Array type. You are trying to call .flat, which is a part of Array.prototype. Since arg could not be of type Array, it is unable to find .flat, thus leading to that error.

I am not certain though. Can you check the object type? You can use this code here below temporarily.

const mixer = (...arg) => { console.log(typeof arg); return []; }

What does it say? If it says Array, then you might operate on a platform that does not support that function yet. Using a polyfill can solve your problem here.

Community
  • 1
  • 1
KarelG
  • 5,176
  • 4
  • 33
  • 49
  • the `react-native` transpiler interpreted `...arg`, it can not understand what is `flat`. –  Oct 18 '18 at 10:06