我正在初始加载数据。当用户单击按钮添加识别时,api调用将其添加并返回。我将新的帖子添加到数组中,但是新的更新不呈现。返回对象和对象数组是相同的对象类型。当我重新加载页面时,将显示新帖子,只是不在add函数上。有什么我想念的吗?
const [recognitions, setRecognitions] = useState([]);
useEffect(() => {
Api.GetRecognitions(params)
.then(response => {
const items = response || [];
setRecognitions(recognitions => [...recognitions, ...items]);
})
}, [setRecognitions]);
const handleAddPost = () => {
Api.AddRecognition(params)
.then(response => {
const newPost = response;
setRecognitions(recognitions=> [...recognitions, newPost])
});
}
<Form.Group>
<Form.Field>
<Button basic color='blue' onClick={handleAddPost}>Add</Button>
</Form.Field>
</Form.Group>
<Form.Group>
<Form.Field>
{recognitions.map(recognition => (
<RecogWallPost
key={recognition.recogStagingId}
recognition={recognition}
participantId={participantId}
/>
)
)}
</Form.Field>
</Form.Group>
答案 0 :(得分:2)
您想传递[setRecognitions]
,而不是传递useEffect
作为[recognitions]
的第二个参数。这告诉useEffect
挂钩在每次识别更改时运行,它在handleAddPost
内部执行。
答案 1 :(得分:0)
您必须创建一个异步函数,然后按以下方式使用它:
import requests
params = {
'switchLocale': 'y',
'siteEntryPassthrough': 'true'
}
def main(url, params):
r = requests.get(url, params=params)
with open("test.pdf", 'wb') as f:
f.write(r.content)
main("https://www.blackrock.com/uk/individual/literature/annual-report/blackrock-index-selection-fund-en-gb-annual-report-2019.pdf", params)