React Native `new Function()` does not support ES6 syntax











up vote
0
down vote

favorite












CMD:



react-native init Test && react-native run-android



App.js:



export default class App extends Component {
render() {
new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });
}
}


Whenever the new function gets constructed and invoked, the app crashes stating: "SyntaxError: Unexpected token '{'. Expected an identifier name in const declaration" Only happens on Android.



Any help would be appreciated. Thanks!





React Native: v0.55.7










share|improve this question


























    up vote
    0
    down vote

    favorite












    CMD:



    react-native init Test && react-native run-android



    App.js:



    export default class App extends Component {
    render() {
    new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });
    }
    }


    Whenever the new function gets constructed and invoked, the app crashes stating: "SyntaxError: Unexpected token '{'. Expected an identifier name in const declaration" Only happens on Android.



    Any help would be appreciated. Thanks!





    React Native: v0.55.7










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      CMD:



      react-native init Test && react-native run-android



      App.js:



      export default class App extends Component {
      render() {
      new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });
      }
      }


      Whenever the new function gets constructed and invoked, the app crashes stating: "SyntaxError: Unexpected token '{'. Expected an identifier name in const declaration" Only happens on Android.



      Any help would be appreciated. Thanks!





      React Native: v0.55.7










      share|improve this question













      CMD:



      react-native init Test && react-native run-android



      App.js:



      export default class App extends Component {
      render() {
      new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });
      }
      }


      Whenever the new function gets constructed and invoked, the app crashes stating: "SyntaxError: Unexpected token '{'. Expected an identifier name in const declaration" Only happens on Android.



      Any help would be appreciated. Thanks!





      React Native: v0.55.7







      android react-native ecmascript-6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 17:35









      Jediah Dizon

      33




      33
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          The react native documentation indicates that JavaScriptCore is generally used at runtime (V8 during debugging), but it's light on specifics as to how it is configured. One thing it does mention is that the native JavaScriptCore is used on iOS, while a different version is bundled with the app for user on Android.



          Because Babel is used at compile time with react native to support ES5/ES6 features, it may be that the runtime is configured at a lower level of support. So when attempting to create code from a string at runtime, you may actually be running with a JavaScript interpreter that doesn't understand e.g., destructuring syntax.



          You could try using Babel's transform at runtime to transpile your code:



          import {transform} from 'babel-core';

          export default class App extends Component {
          render() {
          const f = 'const { firstname } = person; alert(firstname);';

          const result = transform(f, {
          presets: ['es2015']
          });

          new Function("person", result.code)({ firstname: "Test" });
          }
          }





          share|improve this answer





















          • Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
            – Jediah Dizon
            Nov 22 at 18:52










          • @JediahDizon there is any reason to create a function inside the render method.
            – Helmer Barcos
            Nov 25 at 16:00


















          up vote
          -1
          down vote













          Can you create a const with a dynamic name? If it's possible, sorry about my lack of knowledge about that subject.
          The error message said that a name to variable const is expected.
          I hope it has been useful.
          Best regards.






          share|improve this answer




























            up vote
            -1
            down vote













            Try to change the style you create that function. In React Native is common to see arrow functions, that must be created outside your render method.
            Note that your render method will be trigger every single time that your state changes. and it would be a waste of memory resources and unnecessary computing time



            import React, { 
            Component
            } from 'react';

            import {
            Text,
            View,
            StyleSheet
            } from 'react-native';


            export default class App extends Component {

            //your custom function
            myFunc = (param) => {
            console.log(param)
            return param
            }

            //your render method
            render() {
            const param = "Im a text"

            //you could do this... i would never do that..
            const myFuncInRender = () => { console.log('Im a stupid func')}
            const myStupidFunc2 = new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });

            return (
            <View style={styles.container}>
            <Text style={styles.paragraph}>
            {this.myFunc(param)/* HERE is where you call the func*/}
            </Text>
            </View>
            );
            }
            } // end from Class

            const styles = StyleSheet.create({
            container: {
            flex: 1,
            justifyContent: 'center',
            backgroundColor: '#ecf0f1',
            padding: 8,
            alignItems:'center',
            },
            paragraph: {
            margin: 24,
            fontSize: 18,
            fontWeight: 'bold',
            textAlign: 'center',
            },
            });





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435941%2freact-native-new-function-does-not-support-es6-syntax%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              The react native documentation indicates that JavaScriptCore is generally used at runtime (V8 during debugging), but it's light on specifics as to how it is configured. One thing it does mention is that the native JavaScriptCore is used on iOS, while a different version is bundled with the app for user on Android.



              Because Babel is used at compile time with react native to support ES5/ES6 features, it may be that the runtime is configured at a lower level of support. So when attempting to create code from a string at runtime, you may actually be running with a JavaScript interpreter that doesn't understand e.g., destructuring syntax.



              You could try using Babel's transform at runtime to transpile your code:



              import {transform} from 'babel-core';

              export default class App extends Component {
              render() {
              const f = 'const { firstname } = person; alert(firstname);';

              const result = transform(f, {
              presets: ['es2015']
              });

              new Function("person", result.code)({ firstname: "Test" });
              }
              }





              share|improve this answer





















              • Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
                – Jediah Dizon
                Nov 22 at 18:52










              • @JediahDizon there is any reason to create a function inside the render method.
                – Helmer Barcos
                Nov 25 at 16:00















              up vote
              0
              down vote



              accepted










              The react native documentation indicates that JavaScriptCore is generally used at runtime (V8 during debugging), but it's light on specifics as to how it is configured. One thing it does mention is that the native JavaScriptCore is used on iOS, while a different version is bundled with the app for user on Android.



              Because Babel is used at compile time with react native to support ES5/ES6 features, it may be that the runtime is configured at a lower level of support. So when attempting to create code from a string at runtime, you may actually be running with a JavaScript interpreter that doesn't understand e.g., destructuring syntax.



              You could try using Babel's transform at runtime to transpile your code:



              import {transform} from 'babel-core';

              export default class App extends Component {
              render() {
              const f = 'const { firstname } = person; alert(firstname);';

              const result = transform(f, {
              presets: ['es2015']
              });

              new Function("person", result.code)({ firstname: "Test" });
              }
              }





              share|improve this answer





















              • Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
                – Jediah Dizon
                Nov 22 at 18:52










              • @JediahDizon there is any reason to create a function inside the render method.
                – Helmer Barcos
                Nov 25 at 16:00













              up vote
              0
              down vote



              accepted







              up vote
              0
              down vote



              accepted






              The react native documentation indicates that JavaScriptCore is generally used at runtime (V8 during debugging), but it's light on specifics as to how it is configured. One thing it does mention is that the native JavaScriptCore is used on iOS, while a different version is bundled with the app for user on Android.



              Because Babel is used at compile time with react native to support ES5/ES6 features, it may be that the runtime is configured at a lower level of support. So when attempting to create code from a string at runtime, you may actually be running with a JavaScript interpreter that doesn't understand e.g., destructuring syntax.



              You could try using Babel's transform at runtime to transpile your code:



              import {transform} from 'babel-core';

              export default class App extends Component {
              render() {
              const f = 'const { firstname } = person; alert(firstname);';

              const result = transform(f, {
              presets: ['es2015']
              });

              new Function("person", result.code)({ firstname: "Test" });
              }
              }





              share|improve this answer












              The react native documentation indicates that JavaScriptCore is generally used at runtime (V8 during debugging), but it's light on specifics as to how it is configured. One thing it does mention is that the native JavaScriptCore is used on iOS, while a different version is bundled with the app for user on Android.



              Because Babel is used at compile time with react native to support ES5/ES6 features, it may be that the runtime is configured at a lower level of support. So when attempting to create code from a string at runtime, you may actually be running with a JavaScript interpreter that doesn't understand e.g., destructuring syntax.



              You could try using Babel's transform at runtime to transpile your code:



              import {transform} from 'babel-core';

              export default class App extends Component {
              render() {
              const f = 'const { firstname } = person; alert(firstname);';

              const result = transform(f, {
              presets: ['es2015']
              });

              new Function("person", result.code)({ firstname: "Test" });
              }
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 at 18:24









              Myk Willis

              5,82011936




              5,82011936












              • Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
                – Jediah Dizon
                Nov 22 at 18:52










              • @JediahDizon there is any reason to create a function inside the render method.
                – Helmer Barcos
                Nov 25 at 16:00


















              • Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
                – Jediah Dizon
                Nov 22 at 18:52










              • @JediahDizon there is any reason to create a function inside the render method.
                – Helmer Barcos
                Nov 25 at 16:00
















              Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
              – Jediah Dizon
              Nov 22 at 18:52




              Though babel-core isn't supported in React Native, atleast this clears things up. Thanks for the help.
              – Jediah Dizon
              Nov 22 at 18:52












              @JediahDizon there is any reason to create a function inside the render method.
              – Helmer Barcos
              Nov 25 at 16:00




              @JediahDizon there is any reason to create a function inside the render method.
              – Helmer Barcos
              Nov 25 at 16:00












              up vote
              -1
              down vote













              Can you create a const with a dynamic name? If it's possible, sorry about my lack of knowledge about that subject.
              The error message said that a name to variable const is expected.
              I hope it has been useful.
              Best regards.






              share|improve this answer

























                up vote
                -1
                down vote













                Can you create a const with a dynamic name? If it's possible, sorry about my lack of knowledge about that subject.
                The error message said that a name to variable const is expected.
                I hope it has been useful.
                Best regards.






                share|improve this answer























                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  Can you create a const with a dynamic name? If it's possible, sorry about my lack of knowledge about that subject.
                  The error message said that a name to variable const is expected.
                  I hope it has been useful.
                  Best regards.






                  share|improve this answer












                  Can you create a const with a dynamic name? If it's possible, sorry about my lack of knowledge about that subject.
                  The error message said that a name to variable const is expected.
                  I hope it has been useful.
                  Best regards.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 17:58









                  Alvondi Junior

                  12




                  12






















                      up vote
                      -1
                      down vote













                      Try to change the style you create that function. In React Native is common to see arrow functions, that must be created outside your render method.
                      Note that your render method will be trigger every single time that your state changes. and it would be a waste of memory resources and unnecessary computing time



                      import React, { 
                      Component
                      } from 'react';

                      import {
                      Text,
                      View,
                      StyleSheet
                      } from 'react-native';


                      export default class App extends Component {

                      //your custom function
                      myFunc = (param) => {
                      console.log(param)
                      return param
                      }

                      //your render method
                      render() {
                      const param = "Im a text"

                      //you could do this... i would never do that..
                      const myFuncInRender = () => { console.log('Im a stupid func')}
                      const myStupidFunc2 = new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });

                      return (
                      <View style={styles.container}>
                      <Text style={styles.paragraph}>
                      {this.myFunc(param)/* HERE is where you call the func*/}
                      </Text>
                      </View>
                      );
                      }
                      } // end from Class

                      const styles = StyleSheet.create({
                      container: {
                      flex: 1,
                      justifyContent: 'center',
                      backgroundColor: '#ecf0f1',
                      padding: 8,
                      alignItems:'center',
                      },
                      paragraph: {
                      margin: 24,
                      fontSize: 18,
                      fontWeight: 'bold',
                      textAlign: 'center',
                      },
                      });





                      share|improve this answer



























                        up vote
                        -1
                        down vote













                        Try to change the style you create that function. In React Native is common to see arrow functions, that must be created outside your render method.
                        Note that your render method will be trigger every single time that your state changes. and it would be a waste of memory resources and unnecessary computing time



                        import React, { 
                        Component
                        } from 'react';

                        import {
                        Text,
                        View,
                        StyleSheet
                        } from 'react-native';


                        export default class App extends Component {

                        //your custom function
                        myFunc = (param) => {
                        console.log(param)
                        return param
                        }

                        //your render method
                        render() {
                        const param = "Im a text"

                        //you could do this... i would never do that..
                        const myFuncInRender = () => { console.log('Im a stupid func')}
                        const myStupidFunc2 = new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });

                        return (
                        <View style={styles.container}>
                        <Text style={styles.paragraph}>
                        {this.myFunc(param)/* HERE is where you call the func*/}
                        </Text>
                        </View>
                        );
                        }
                        } // end from Class

                        const styles = StyleSheet.create({
                        container: {
                        flex: 1,
                        justifyContent: 'center',
                        backgroundColor: '#ecf0f1',
                        padding: 8,
                        alignItems:'center',
                        },
                        paragraph: {
                        margin: 24,
                        fontSize: 18,
                        fontWeight: 'bold',
                        textAlign: 'center',
                        },
                        });





                        share|improve this answer

























                          up vote
                          -1
                          down vote










                          up vote
                          -1
                          down vote









                          Try to change the style you create that function. In React Native is common to see arrow functions, that must be created outside your render method.
                          Note that your render method will be trigger every single time that your state changes. and it would be a waste of memory resources and unnecessary computing time



                          import React, { 
                          Component
                          } from 'react';

                          import {
                          Text,
                          View,
                          StyleSheet
                          } from 'react-native';


                          export default class App extends Component {

                          //your custom function
                          myFunc = (param) => {
                          console.log(param)
                          return param
                          }

                          //your render method
                          render() {
                          const param = "Im a text"

                          //you could do this... i would never do that..
                          const myFuncInRender = () => { console.log('Im a stupid func')}
                          const myStupidFunc2 = new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });

                          return (
                          <View style={styles.container}>
                          <Text style={styles.paragraph}>
                          {this.myFunc(param)/* HERE is where you call the func*/}
                          </Text>
                          </View>
                          );
                          }
                          } // end from Class

                          const styles = StyleSheet.create({
                          container: {
                          flex: 1,
                          justifyContent: 'center',
                          backgroundColor: '#ecf0f1',
                          padding: 8,
                          alignItems:'center',
                          },
                          paragraph: {
                          margin: 24,
                          fontSize: 18,
                          fontWeight: 'bold',
                          textAlign: 'center',
                          },
                          });





                          share|improve this answer














                          Try to change the style you create that function. In React Native is common to see arrow functions, that must be created outside your render method.
                          Note that your render method will be trigger every single time that your state changes. and it would be a waste of memory resources and unnecessary computing time



                          import React, { 
                          Component
                          } from 'react';

                          import {
                          Text,
                          View,
                          StyleSheet
                          } from 'react-native';


                          export default class App extends Component {

                          //your custom function
                          myFunc = (param) => {
                          console.log(param)
                          return param
                          }

                          //your render method
                          render() {
                          const param = "Im a text"

                          //you could do this... i would never do that..
                          const myFuncInRender = () => { console.log('Im a stupid func')}
                          const myStupidFunc2 = new Function("person", "const { firstname } = person; alert(firstname);")({ firstname: "Test" });

                          return (
                          <View style={styles.container}>
                          <Text style={styles.paragraph}>
                          {this.myFunc(param)/* HERE is where you call the func*/}
                          </Text>
                          </View>
                          );
                          }
                          } // end from Class

                          const styles = StyleSheet.create({
                          container: {
                          flex: 1,
                          justifyContent: 'center',
                          backgroundColor: '#ecf0f1',
                          padding: 8,
                          alignItems:'center',
                          },
                          paragraph: {
                          margin: 24,
                          fontSize: 18,
                          fontWeight: 'bold',
                          textAlign: 'center',
                          },
                          });






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 22 at 18:11

























                          answered Nov 22 at 17:59









                          Helmer Barcos

                          57029




                          57029






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435941%2freact-native-new-function-does-not-support-es6-syntax%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                              Alexandru Averescu

                              Trompette piccolo