Android React Native`new Function()`不支持ES6语法

Android React Native`new Function()`不支持ES6语法,android,react-native,ecmascript-6,Android,React Native,Ecmascript 6,命令: 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" }); } } 每当构造和调用新函数

命令:

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" });
    }
}
每当构造和调用新函数时,应用程序就会崩溃,并声明:
“SyntaxError:Unexpected token'{'。const声明中应包含标识符名称”
仅在Android上发生

任何帮助都将不胜感激。谢谢



React Native:v0.55.7

您能创建一个具有动态名称的常量吗?如果可能,很抱歉我对该主题缺乏了解。 错误消息表示应为变量const指定一个名称。 我希望这是有用的。
致以最诚挚的问候。

尝试更改创建该函数的样式。在React Native中,常见的箭头函数必须在渲染方法之外创建。 请注意,每次状态更改时都会触发渲染方法。这将浪费内存资源和不必要的计算时间

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',
  },
});
import-React,{
组成部分
}从"反应",;
导入{
文本
看法
样式表
}从“反应本机”;
导出默认类应用程序扩展组件{
//您的自定义函数
myFunc=(参数)=>{
console.log(参数)
返回参数
}
//您的渲染方法
render(){
const param=“我是一个文本”
//你可以这样做…我永远不会那样做。。
const myFuncInRender=()=>{console.log('ima bid func')}
const myStupidFunc2=新函数(“person”,“const{firstname}=person;alert(firstname);”)({firstname:“Test”});
返回(
{this.myFunc(param)/*这里是调用func*/}
);
}
}//下课
const styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
背景颜色:“#ecf0f1”,
填充:8,
对齐项目:'中心',
},
第段:{
差额:24,
尺码:18,
fontWeight:'粗体',
textAlign:'中心',
},
});
react native表示它通常在运行时使用(调试期间为V8),但它对如何配置它的细节很清楚。它确实提到的一点是,本机JavaScriptCore在iOS上使用,而Android上的用户应用程序捆绑了一个不同的版本

由于在编译时与react native一起使用以支持ES5/ES6功能,因此可能是在较低级别的支持下配置了运行时。因此,当尝试在运行时从字符串创建代码时,您可能实际使用的JavaScript解释器不理解(例如,解构语法)

您可以尝试在运行时使用Babel来传输代码:

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" });
  }
}

尽管React Native中不支持babel core,但这至少可以解决问题。感谢您的帮助。@JediahDizon有任何理由在render方法中创建函数。