Javascript 使用ref对焦,但使用条件设置ref
上面的代码会有错误,如何根据从父组件传递来的标志聚焦子组件 您不能用这种逻辑将道具设置为组件。您可以改为执行以下操作之一:Javascript 使用ref对焦,但使用条件设置ref,javascript,reactjs,Javascript,Reactjs,上面的代码会有错误,如何根据从父组件传递来的标志聚焦子组件 您不能用这种逻辑将道具设置为组件。您可以改为执行以下操作之一: <Editor {...other} {this.props.isFocus && ref={input => input && input.focus()} } /> 或 话虽如此,我认为你没有正确使用ref。我不认为只有在某些条件下才有裁判的正当理由;无论应用程序逻辑如何,它都应该是一个静态道具 否则,
<Editor
{...other}
{this.props.isFocus && ref={input => input && input.focus()} }
/>
或
话虽如此,我认为你没有正确使用ref。我不认为只有在某些条件下才有裁判的正当理由;无论应用程序逻辑如何,它都应该是一个静态道具
否则,您很可能会错误地使用ref,但是如果没有看到代码的其余部分,很难说为什么或者如何使用ref。它会抛出什么错误?
<Editor
{...other}
ref={this.props.isFocus ? (input => input && input.focus()) : null}
/>
{this.props.isFocus ? <Editor
{...other}
ref={input => input && input.focus()}
/>
: <Editor {...other} />}