【发布时间】:2019-10-18 10:27:59
【问题描述】:
我正在使用带有 React 功能组件的 material-ui 并使用它的 Autocomplete 组件。我自定义了它,每当我更改输入字段中的文本时,我希望该组件呈现新的搜索结果。
callAPI("xyz")
我在action中调用API,使用xyz参数,从这个函数组件调用dispatch方法。
这里的问题是,当组件进行调用时,它应该等待API响应然后渲染结果,但是它得到了一个未解析的promise,所以它渲染失败。
<Paper square>
{callAPI("xyz").results.map(
result => console.log(result);
)}
</Paper>
由于结果是一个未解决的承诺,它不会映射。 我需要一些方法来仅在数据可用时调用地图,或者在数据存在之前显示一些文本,然后在获取数据后更改。
任何更正此代码的建议都会非常有帮助。
编辑:
function IntegrationDownshift() {
return (
<div>
<Downshift id="downshift-simple">
{({
getInputProps,
getItemProps,
getMenuProps,
highlightedIndex,
inputValue,
isOpen,
selectedItem
}) => (
<div>
{renderInput({
fullWidth: true,
InputProps: getInputProps({
placeholder: "Search users with id"
})
})}
<div {...getMenuProps()}>
{isOpen ?
<Paper square>
{callAPI(inputValue).users.map(
(suggestion, index) =>
renderSuggestion({
suggestion,
index,
itemProps: getItemProps({
item:
suggestion.userName
}),
highlightedIndex,
selectedItem
})
)}
</Paper>
: null}
</div>
</div>
)}
</Downshift>
</div>
);
}
【问题讨论】:
-
调度触发加载器的动作或数据正在加载的一些文本,一旦解决,调度加载器转动和设置数据的动作。在您的 jsx 中,您可以编写一个三元检查 loader 是否为 true 渲染加载器组件,否则渲染数据
-
@DILEEPTHOMAS 您能否为您的示例提供任何伪代码或示例
-
你能提供更多的代码吗,我认为paper是一个定制的组件。因此,在更改时,您将调用一个道具,并且调度在父组件中完成
-
@DILEEPTHOMAS 添加了附加代码以供参考,您可以看到 callAPI 方法采用 inputValue 并返回与用户的 Object。我想在上面应用地图并调用另一个渲染。但是,当它最初加载时,
会在没有数据的情况下呈现,因为 promise 仍未解决,它会因 map 而失败。
标签: javascript reactjs material-ui react-component react-async