本文介绍了如何在FormDataConsumer中使用异步Javascript函数,例如fetch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在FormDataConsumer标记内使用访存,但似乎FormDataConsumer不支持异步功能.这段代码对我不起作用:
I need to use a fetch inside the FormDataConsumer tag but it seems FormDataConsumer does not support async functions. This code didn't work for me:
<FormDataConsumer>
{
async ({ formData, scopedFormData, getSource, ...rest }) => {
return await fetch('/api/v1/attributes/'+scopedFormData.single_attributes_label)
.then(res => res.json())
.then(data => {
console.log(JSON.stringify(data));
//return JSON.stringify(data);
resolve(JSON.stringify(data));
});
return JSON.stringify(scopedFormData);
}
}
</FormDataConsumer>
我也检查了此代码,但此代码也无效:
I also checked this code and this one also didn't work:
async function getAttrItem(id) {
return await fetch(`/api/v1/attributes/${id}`).then(response => response.json())
}
...
<FormDataConsumer>
{
async ({ formData, scopedFormData, getSource, ...rest }) => {
return await JSON.stringify(getAttrItem(scopedFormData.single_attributes_label));
}
}
</FormDataConsumer>
但是当我使用它时,它可以在控制台中工作:
But when I use this one, it works in the console:
<FormDataConsumer>
{
({ formData, scopedFormData, getSource, ...rest }) => {
fetch('/api/v1/attributes/'+scopedFormData.single_attributes_label)
.then(res => res.json())
.then(data => {
console.log(JSON.stringify(data));
//return JSON.stringify(data);
resolve(JSON.stringify(data));
});
return JSON.stringify(scopedFormData);
}
}
</FormDataConsumer>
我应该使用此FormDataConsumer填充对象,然后在另一个FormDataConsumer中检查该对象吗?
Should I use this FormDataConsumer for filling an object and then inside the other FormDataConsumer check the object?
推荐答案
我的问题已通过以下代码解决:
My issue was fixed with this code:
<FormDataConsumer>
{
({ formData, scopedFormData, getSource, ...rest }) => {
return scopedFormData && formData && "type" in formData && <ReferenceInput label="Attribute Label" source={getSource('single_attributes_label')} reference={`attributes`} filter={{ document_type: formData.type }}>
<AutocompleteInput optionText="title" optionValue="id" />
</ReferenceInput>
}
}
</FormDataConsumer>
<FormDataConsumer>
{
({ formData, scopedFormData, getSource, ...rest }) => {
return "type" in formData && "single_attributes_label" in scopedFormData && <AttribValue source={getSource('single_attributes_value')} reference={`attributes`} filter={{ document_type: formData.type, id: scopedFormData.single_attributes_label, method: "getOptions" }} attribute={scopedFormData.single_attributes_label} {...rest} />
}
}
</FormDataConsumer>
const AttribValue = props => {
const [data, setData] = useState();
console.log('props', props);
useEffect(() => {
fetch("/api/v1/attributes/" + props.attribute)
.then(res => res.json())
.then(data => {
setData({ data });
});
}, [props.attribute, props.label]);
if (!data) {
return 'Please Wait...';
}
let newProp = Object.assign({}, props, { label: "Attributes Value" });
return data.data.attribute_type == 'multiselect' ? <ReferenceArrayInput {...newProp}><AutocompleteArrayInput optionText="text" optionValue="id" /></ReferenceArrayInput> : <ReferenceInput {...newProp}><AutocompleteInput optionText="text" optionValue="id" /></ReferenceInput>;
};
这篇关于如何在FormDataConsumer中使用异步Javascript函数,例如fetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!