Javascript 如何将多个道具传递给React.createElement创建的元素?
React.createElement(类型,[props],[children]) 正如react文件所说的那样。但不知何故,多重道具对我不起作用。我试图传递一个键值对象数组,传递一个带有键值的容器对象,但什么都不起作用。让我展示一下我是如何尝试的:Javascript 如何将多个道具传递给React.createElement创建的元素?,javascript,reactjs,Javascript,Reactjs,React.createElement(类型,[props],[children]) 正如react文件所说的那样。但不知何故,多重道具对我不起作用。我试图传递一个键值对象数组,传递一个带有键值的容器对象,但什么都不起作用。让我展示一下我是如何尝试的: //input.js const Input = ({name, elementProps}) => { const { element, props } = elementProps; return React.creat
//input.js
const Input = ({name, elementProps}) => {
const { element, props } = elementProps;
return React.createElement(element, props)
}
export default Input;
//props from
{
name: "msg",
element: "textarea",
props: [
{placeholder: "Message"},
{rows: "4"},
{cols: "50"},
]
}
//rendered by method
const { name, ...elementProps } = objectAbove;
return <Input name={name} elementProps={elementProps} />
//input.js
常量输入=({name,elementProps})=>{
常量{element,props}=elementProps;
return React.createElement(元素、道具)
}
导出默认输入;
//道具
{
名称:“msg”,
元素:“textarea”,
道具:[
{占位符:“消息”},
{行:“4”},
{cols:“50”},
]
}
//通过方法渲染
const{name,…elementProps}=objectover;
返回
传递多个props需要什么语法?
props
应该是非数组对象,而不是数组。您正在创建的道具阵列看起来很容易成为单个对象,而不是对象阵列。而不是:
props: [
{placeholder: "Message"},
{rows: "4"},
{cols: "50"},
]
或许
props: {
placeholder: "Message",
rows: "4",
cols: "50"
}
如中所述,[]
的语法如下所示:
…的意思是表明
props
是可选的,而不是它是一个数组。[props]
文档中的React.createElement(type,[props],[children])
只是表示它是可选的,而不是数组。你的道具应该是一个物体<代码>{占位符:“消息”,行:“4”,列:“50”}
React.createElement( type, [props], [...children] )