我想把一个数组属性转换成一个字符串:
export default class MyComp extends React.Component {
render() {
let type = this.props.type; // [".abc",".ded",".ted"];
newType = type.join();
//o/p: newType= ".abc,.ded,.ted"
console.log(newType) // ".abc,.ded,.ted"
return (
<div>
<input type="file" accept={newType}/> //here throws error
</div>
)
}
}
export default class SubComp extends React.Component{
render() {
<Mycomp type={[".abc",".ded",".ted"]}/>
}
}
当我试图访问作为accept
html的值的newType
时,它抛出:TypeError: Cannot read property 'join' of undefined
。我在这里检查的一件事是,如果我试图将硬编码值传递给newType
,它就能正常工作。只有当我尝试使用.join()
或.toString()
将数组转换为字符串时,它才会失败。
render() {
let type = this.props.type; // [".abc",".ded",".ted"];
newType = ".abc,.ded,.ted";
return (
<div>
<input type="file" accept={newType}/> //Works Fine!!!!
</div>
)
}
你知道是什么导致了这个问题吗?
发布于 2018-10-11 05:01:33
您需要使用花括号{}
设置您的类型属性,如下所示:
<Mycomp type={[".abc",".ded",".ted"]}/>
发布于 2018-10-11 05:01:42
你应该像这样把数据传递给道具
export default class MyComp extends React.Component {
render() {
let type = this.props.type; // [".abc",".ded",".ted"];
let newType = type.join()//converts to string: ".abc,.ded,.ted"
return (
<div>
<input type="file" accept={newType}/> //here throws error
</div>
)
}
}
export default class SubComp extends React.Component{
render() {
<Mycomp type={[".abc",".ded",".ted"]}/>
}
}
发布于 2018-10-11 05:03:15
尝试将数组传递给属性,如下所示
<Mycomp type={[".abc",".ded",".ted"]} />
https://stackoverflow.com/questions/52748581
复制相似问题