【发布时间】:2020-06-24 03:45:26
【问题描述】:
0
我在 React 中使用 useState 时遇到了一些问题。我有一个自定义组件Dialog,它可以传递renderAction 和renderContent 作为道具。
所以我设法打开了第一个对话框,一切看起来都井井有条,这是代码底部的<Dialog> 标签。但是在我从服务器得到响应后,我想显示第二个 Dialog 弹出窗口并关闭第一个 Dialog 弹出窗口。但在第一个消失后,第二个永远不会弹出。
服务器调用成功,所以response.success为真,这就是为什么setPopupOpen=false(关闭第一个弹出窗口)有效,但我不知道为什么,因为我设置了secondPopupOpen=true,但为什么同样的方法将无法显示第二个对话框弹出窗口。
AppNav.js
const AppNav = () => {
//...
const [popupOpen, setPopupOpen] = useState(false);
const [secondPopupOpen, setSecondPopupOpen] = useState(false);
const [input, setInput] = useState('');
const renderContent = () => {
return (
<form>
<input
type="text"
onChange={e => setInput(e.target.value)}
value={input}
onBlur={() =>delay(() =>setInput(''),150}
placeholder='placeholder here...'
/>
</form>
);
};
const renderAction = () => {
const handleSubmit = async() => {
//some server calls here
// response is what got back from server call
try{
if(response.success){
setPopupOpen(false);
setSecondPopupOpen(true);
return (
<
<Dialog
open={secondPopupOpen}
renderContent={() => <span>Message:</span>}
renderAction={() => <span>operation successful!</span>}
/>
)
}
}
return (
<Button onClick={handleSubmit}>Submit</Button>
);
};
return (
<Dialog
open={popupOpen}
renderContent={RenderContent}
renderAction={RenderAction}
/>
)
}
【问题讨论】:
-
能否提供该组件的沙箱链接或完整代码?
标签: javascript html reactjs async-await react-hooks