我有react components
陣列,我試圖將它們輸出:動態呈現組件在反應V15
this.props.data
[
{
key: 1,
element: 'input',
type: 'text',
placeholder: 'Jamie is Sex'
},
{
key: 2,
element: 'input',
type: 'text',
placeholder: 'Jamie is Not Sex'
}
]
input.component.js
import React from 'react';
const input = React.createClass({
render() {
return (
<input type="text" />
);
}
});
export default input;
import components.js
動態代碼
import components from '../../componets';
render() {
return (
<form action="">
{this.props.data.map((item, index) => <{components[item.element]} key={index} data={item}/>)}
</form>
//{components[item.element]} which would reference 'input' from components.js
);
}
輸出
bundle.js:33283 Uncaught Error: Cannot find module "../components/Form/Form.component.js"webpackMissingModule @ bundle.js:33283(anonymous function) @ bundle.js:33283__webpack_require__ @ bundle.js:20(anonymous function) @ bundle.js:32763__webpack_require__ @ bundle.js:20(anonymous function) @ bundle.js:7464__webpack_require__ @ bundle.js:20(anonymous function) @ bundle.js:48__webpack_require__ @ bundle.js:20(anonymous function) @ bundle.js:40(anonymous function) @ bundle.js:43
bundle.js:99 [WDS] Errors while compiling.
hahah我剛看到我自己的佔位符文本,我寫了2小時前,仍然讓我發笑。哦,我生活在一個可怕的生活中:'( –
我相信在這種情況下,您會希望使用React.createElement(組件[item.element],) 來動態創建組件 –
Enjayy
事情是''組件[item.elemet ]'引用我的反應組件,它是使用'const input = React.createClass({'所以我不是那麼正確? –