提问人:Pasang 提问时间:11/9/2023 最后编辑:BergiPasang 更新时间:11/10/2023 访问量:86
如何在模板文本中使用 Async Await
How to use Async Await inside template literal
问:
下面的代码只返回字符串文字。我在某处读到模板字符串中的 Async-Await 需要有顶级 await,但就我而言,我怎样才能获得顶级 await?
import { Autocomplete, TextField } from "@mui/material";
import usePlacesAutocomplete, {getGeocode, getZipCode} from "use-places-autocomplete";
const Places = () => {
<Autocomplete
renderOption={(props, option) =>
<li {...props}>
{`${ async () => await getGeocode({placeId: option.place_id})
.then(results => getZipCode(results[0], true))()}
`}
</li> }
/>
}
答:
-2赞
Arsany Benyamine
11/9/2023
#1
我认为您应该从字符串文字中提取异步请求
可以创建一个名为 Option 的新组件,该组件将 api 调用所需的参数作为 Props,并在 useEffect 中执行 api 调用
评论
renderOption
async
<Autocomplete>