简体   繁体   English

如何在 React 中将 ref 转发到其子组件并在挂载时更改设置状态?

[英]How to forward ref to its child component in React and change set state on mount?

If anyone has appeared for Testdome react test then you will surely have come across this question.如果有人参加过 Testdome 反应测试,那么您肯定会遇到这个问题。

The TextInput component renders an input element that accepts a ref forwarded to that input element. TextInput 组件呈现一个输入元素,该元素接受转发到该输入元素的引用。 The Input component should accept a focused prop. Input 组件应该接受一个焦点道具。 When the focused prop is changed from false to true, and the input is not focused, it should receive the focus.当focused prop从false变为true,并且输入没有焦点时,应该接收焦点。 Also, on componentDidMount lifecycle if the focused prop is true, then the input should receive the focus.此外,在 componentDidMount 生命周期中,如果 focus prop 为 true,则输入应获得焦点。

 class Input extends React.PureComponent { render() { let { forwardedRef, ...otherProps } = this.props; return <input { ...otherProps } ref = { forwardedRef } />; } } const TextInput = React.forwardRef((props, ref) => { return <Input { ...props } forwardedRef = { ref } /> }); class FocusableInput extends React.Component { ref = React.createRef() render() { return <TextInput ref = { this.ref } />; } // When the focused prop is changed from false to true, // and the input is not focused, it should receive focus. // If focused prop is true, the input should receive the focus. // Implement your solution below: componentDidUpdate(prevProps) { // The lifecycle will occur when component is mounted // If mounted then it will compare the current props with prevProps // If true then set the focus for the ref else false if (prevProps.focused !== this.props.focused) { this.ref.current.focus() } } componentDidMount() { // This lifecycle checks if component has mount for child // If mount then chck if child has focus if yes set the state to true/ false // for the current ref if (this.props.focused) { this.setState({ focused: this.ref.current.focus() }) } } } FocusableInput.defaultProps = { focused: false }; const App = (props) => < FocusableInput focused = { props.focused } />; document.body.innerHTML = "<div id='root'></div>"; const rootElement = document.getElementById("root"); ReactDOM.render( < App / > , rootElement);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.3.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.3.0/umd/react-dom.production.min.js"></script>

 componentDidUpdate(prevProps) { // The lifecycle will occur when component is mounted // If mounted then it will compare the current props with prevProps // If true then set the focus for the ref else false if( prevProps.focused !== this.props.focused ) { this.ref.current.focus() } } componentDidMount() { // This lifecycle checks if component has mount for child // If mount then chck if child has focus if yes set the state to true/ false // for the current ref if(this.props.focused) { this.setState({ focused:this.ref.current.focus() }) } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM