我正在使用react,并且我需要在列表中添加文件。
this.state = {
some: {
names: [],
keys: 0
}
file: {
images: []
}
}
newSome = () => {
const some = {
...this.state.some,
['name'] = 'inputed name',
[keys] = this.state.some.keys++
}
handleChange = (field,key) => event => {
const file = {...this.state.file}
file['images'].push({[key] : event.target.files[event.target.files.length-1]})
this.setState({file})
}
render(){
const {key} = this.state.some
return (
<input type='file' onChange={this.handleChange('images', key)}/>
)
}
我有一个dictionary
,具有“带有图像的名称”和“没有图像的名称”。在images
中,我有名字的图像。例如:name['maria', 'joao', renato]
,而joao没有图像...因此在图像中我有images[{0: file0},{2: file1}]
。当我加载图像时,在第一个操作中它可以工作,但是在第二个操作中却不起作用,并显示错误TypeError: file.images.push is not a function
。我该怎么办?
答案 0 :(得分:0)
state = {
some: {
names: [],
key: ""
},
file: {
images: []
}
};
handleChange = (field, key) => event => {
const { files } = event.target;
const file = {
images: [...this.state.file.images, { [key]: files[files.length - 1] }]
};
this.setState({ file });
};
render() {
const { key } = this.state.some;
return <input type="file" onChange={this.handleChange("images", key)} />;
}