【发布时间】:2020-08-21 03:32:28
【问题描述】:
我是新手,我正在尝试建立一个网站。我想从父组件触发子组件功能。即我有一个对话框组件和一个导航栏组件。 Navbar(child) 中的 Onclick 事件将 props 发送到 Display(parent) 并且该父组件必须触发 Dialogbox(child) 组件中的功能要启动对话框,并且此对话框必须根据用户输入将我的 true 或 false 返回给父组件。我已经设法做到了。真正的问题是我得到了
index.js:1 Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Transition which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here
in div (created by Transition)
in Transition (created by ForwardRef(Fade))
in ForwardRef(Fade) (created by ForwardRef(Backdrop))
in ForwardRef(Backdrop) (created by WithStyles(ForwardRef(Backdrop)))
in WithStyles(ForwardRef(Backdrop)) (created by ForwardRef(Modal))
in div (created by ForwardRef(Modal))
in ForwardRef(Portal) (created by ForwardRef(Modal))
in ForwardRef(Modal) (created by ForwardRef(Dialog))
in ForwardRef(Dialog) (created by WithStyles(ForwardRef(Dialog)))
in WithStyles(ForwardRef(Dialog)) (at Dialogbox.js:48)
in div (at Dialogbox.js:47)
in AlertDialog (at Display.js:126)
in div (at Display.js:120)
in Router (created by BrowserRouter)
in BrowserRouter (at Display.js:119)
in Display (at App.js:10)
in div (at App.js:9)
in App (at src/index.js:12)
in StrictMode (at src/index.js:11)
控制台中的警告。我不想忽略这个警告。这是我的父组件,
class Display extends React.Component
{
constructor()
{
super()
this.state = {language: english, changedLang: "null", isBackdrop: false}
this.DialogRef = React.createRef();
}
languageSwitch(changedLanguage)
{
if(this.state.language.lang === "en" && changedLanguage === "jp")
{
this.setState({changedLang: changedLanguage})
this.DialogRef.current.handleClickOpen()
// this.refs.child.handleClickOpen()
}
else if(this.state.language.lang === "jp" && changedLanguage === "en")
{
this.setState({changedLang: changedLanguage})
this.DialogRef.current.handleClickOpen()
// this.refs.child.handleClickOpen()
}
}
agreedOrNot(result)
{
if(this.state.language.lang === "en" && this.state.changedLang === "jp")
{
if(result)
{
this.setState({isBackdrop: true})
setTimeout(()=> {this.setState({isBackdrop: false})}, 2500)
setTimeout(()=> {this.setState({language: japanese})}, 2000)
}
}
else if(this.state.language.lang === "jp" && this.state.changedLang === "en")
{
if(result)
{
this.setState({isBackdrop: true})
setTimeout(()=> {this.setState({isBackdrop: false})}, 2500)
setTimeout(()=> {this.setState({language: english})}, 2000)
}
}
return result
}
render()
{
let backdrop
if(this.state.isBackdrop)
{
backdrop = <LoadingScreen />
}
else
{
backdrop = <p/>
}
return(
<div>
<Navbar data={{language: this.state.language,
languageSwitch: this.languageSwitch.bind(this)}} />
{backdrop}
<Dialogbox ref={this.DialogRef} data={{language: this.state.language,
agreedOrNot: this.agreedOrNot.bind(this)}} />
</div>
)
}
}
export default Display
这是我的对话框组件。
class AlertDialog extends React.Component
{
constructor(props)
{
super(props)
this.state = {open: false, loading: false}
this.handleClickOpen = this.handleClickOpen.bind(this)
this.handleCloseFalse = this.handleCloseFalse.bind(this)
this.handleCloseTrue = this.handleCloseTrue.bind(this)
}
handleClickOpen()
{
this.setState({open: true})
}
handleCloseFalse = () =>
{
this.setState({open: false})
this.props.data.agreedOrNot(false)
}
handleCloseTrue = () =>
{
this.setState({open: false})
this.props.data.agreedOrNot(true)
}
render()
{
let currentLang = this.props.data.language.dialogbox
return (
<div>
<Dialog open={this.state.open} onClose={this.handleClose}
aria-labelledby="alert-dialog-title" aria-describedby="alert-dialog-description" >
<DialogTitle id="alert-dialog-title">{currentLang.areYouSure}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description"> {currentLang.description} </DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={this.handleCloseFalse} color="primary"> {currentLang.disagree} </Button>
<Button onClick={this.handleCloseTrue} color="primary" autoFocus> {currentLang.agree} </Button>
</DialogActions>
</Dialog>
</div>
);
}
}
export default AlertDialog
真正的事情是我在功能组件中看到了 React.forwardRef() 的实现,而类中的实现对我来说有点难以正确理解。有人可以帮我在类中实现 React.forwardRef() 吗?
如果可以,有人可以告诉我一种更好的方法来以更简单的方式实现这些操作吗?如果是,请在我的githubhttps://github.com/kishorecmg/kishorecmg做同样的事情
谢谢。
【问题讨论】:
-
虽然我用错了 ref 关键字,但这里似乎可以看到@material-ui/core,github.com/mui-org/material-ui/issues/13394
标签: javascript html reactjs web react-router