我有一个由 API 创建的 JSON 字符串:
var appStructure = {
"code": 200,
"message": "Register Form Loaded.",
"response": "'div',{},'Registration'"
};
我想使用 API response
动态创建元素。我正在尝试做这样的事情:
var testCard1 = React.createElement(appStructure.response);
ReactDOM.render(testCard1, document.getElementById('main-content'));
但它一直给我错误
Warning: is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements. Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('h1',{},'Hello Testing') is not a valid name.
最佳答案
appStructure.response
是字符串,无法作为参数传递到 createElement
方法中。您应该将字符串更改为可接受的参数。另外 testCard1
应该采用 PascalCase:TestCard1
var args = appStructure.response.split(',');
var TestCard1= React.createElement(args[0].replace(/\W/g, ''), JSON.parse(args[1]), args[2].replace(/\W/g, ''));
关于json - 传递 JSON 字符串来创建标签 React.createelement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000677/