英文:
React - how to add N number of data properties to a reusable functional component
问题
以下是您要求的代码部分的翻译:
//
export default function Input({
dataAttributes=null,
})
return (
<input class="" {...dataAttributes}/>
);
}
import Input from "./Input"
<Input dataAttributes={'data-title':'this title', 'data-someting':'this is something else', ...add more}/>
to dom
<input data-title="this title" data-something="this is something else" ...whatever else was added/>
英文:
Is this something that is possible? I keep getting some weird errors, again the dataAttribute should not be finite.
//
export default function Input({
dataAttributes=null,
})
return (
<input class="" {...dataAttributes}/>
);
}
import Input from "./Input"
<Input dataAttributes={'data-title':'this title', 'data-someting':'this is something else', ...add more}/>
to dom
<input data-title="this title" data-something="this is something else" ...whatever else was added/>
答案1
得分: 2
需要在Input标签内使用双括号。第一对用于在标签内编写JavaScript,第二对用于声明对象。
<Input dataAttributes={{'data-title':'this title', 'data-someting':'this is something else', ...add more}}/>
英文:
You need to use double brackets inside the Input tag. The first pair is to write javascript inside tags, the second pair are for the object you're declaring.
<Input dataAttributes={{'data-title':'this title', 'data-someting':'this is something else', ...add more}}/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论