当我导航到特定页面时,我希望用我已经上传的图像重新填充文件池图像,并且图像名称在数据库中。
当前,我已对映像名称进行了硬编码,该映像名称在componentDidMount中设置的状态下可用,但该状态也在文件池中设置,这使我有些困惑。
因此,这是硬编码的时间:
class HomeBannerForm extends InputForm {
async componentDidMount() {
const { data: hero } = await getHero();
this.setState({ data: this.mapToViewModel(hero) });
}
state = {
data: {
title: "",
bgImg: ""
},
errors: {},
files: [
{
source: "myImage.jpg",
options: {
type: "local"
}
}
]
};
mapToViewModel(hero) {
return {
id: hero._id,
title: hero.title,
bgImg: hero.bgImg
};
}
所以,我需要将myImage.jpg更改为componentDidMount的状态,即:数据库的名称。这显然行不通。
files: [
{
source: this.state.data.bgImg
options: {
type: "local"
}
}
]
Filepond组件代码:
<FilePond
ref={ref => (this.pond = ref)}
files={this.state.files}
allowMultiple={false}
maxFiles={1}
instantUpload={false}
name="bgImg"
server={{
process: "http://localhost:8000/api/hero/",
load: "http://localhost:8000/img/"
}}
oninit={() => this.handleInit()}
onupdatefiles={fileItems => {
// Set currently active file objects to this.state
this.setState({
files: fileItems.map(fileItem => fileItem.file)
});
}}
// callback for successfully uploaded image
onprocessfile={() => this.uploadComplete()}
/>
答案 0 :(得分:1)
看起来您需要调整英雄映射:
mapToState(hero) {
return {
data: {
title: hero.title,
bgImg: hero.bgImg
},
errors: {},
files: [
{
source: hero.bgImg,
options: {
type: "local"
}
}
]
};
}
async componentDidMount() {
const { data: hero } = await getHero();
this.setState(this.mapToState(hero));
}
答案 1 :(得分:0)
尝试一下。
class HomeBannerForm extends InputForm {
state = {
data: {
title: "",
bgImg: ""
},
errors: {},
files: [
{
source: "myImage.jpg",
options: {
type: "local"
}
}
]
};
async componentDidMount() {
const { data: hero} = await getHero();
this.setState(prevState => ({
data: this.mapToViewModel(hero),
files: prevState.files.map(obj => {
Object.assign(obj, { source: hero.bgImg });
})
}));
}
mapToViewModel(hero) {
return {
id: hero._id,
title: hero.title,
bgImg: hero.bgImg
};
}