第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React 生命周期內(nèi)文檔上的 React Keyup 事件

React 生命周期內(nèi)文檔上的 React Keyup 事件

達(dá)令說 2023-07-14 14:45:57
據(jù)我了解,refs 不是在 React 生命周期(source)之外定義的。我試圖解決的問題是捕獲文檔級別的按鍵(即,無論聚焦哪個(gè)元素都觸發(fā)事件),然后與反應(yīng)引用交互。下面是我正在嘗試做的事情的簡化示例:export default class Console extends React.Component {??? ? constructor(props) {? ? ? ? super(props);? ? ? ? this.state = {? ? ? ? ? ? visible: false,? ? ? ? ? ? text: "",? ? ? ? };? ? }? ? print(output: string) {? ? ? ? this.setState({? ? ? ? ? ? text: this.state.text + output + "\n"? ? ? ? })? ? }? ? toggleVisible()? ? {? ? ? ? this.setState({visible: !this.state.visible});? ? }? ? render() {? ? ? ? const footer_style = {? ? ? ? ? ? display: this.state.visible ? "inline" : "none",? ? ? ? };? ? ? ? return (? ? ? ? ? ? <footer id="console-footer" className="footer container-fluid fixed-bottom" style={footer_style}>? ? ? ? ? ? ? ? <div className="row">? ? ? ? ? ? ? ? ? ? <textarea id="console" className="form-control" rows={5} readOnly={true}>{this.state.text}</textarea>? ? ? ? ? ? ? ? </div>? ? ? ? ? ? </footer>? ? ? ? );? ? }}class App extends React.Component {? private console: Console;? constructor() {? ? super({});? ??? ? this.console = React.createRef();? }? keyDown = (e) =>? {? ? ? this.console.current.toggleVisible(); // <-- this is undefined? }? componentDidMount(){? ? ? document.addEventListener("keydown", this.keyDown);? },? componentWillUnmount() {? ? ? document.removeEventListener("keydown", this.keyDown);? },? render() {? ? return (? ? ? <div className="App" onKeyDown={this.keyDown}> // <-- this only works when this element is in focus? ? ? ? // other that has access to this.console that will call console.print(...)? ? ? ? <Console ref={this.console} />? ? ? </div>? ? );? }}我的問題是:有沒有辦法在 React 的生命周期內(nèi)進(jìn)行此類文檔級按鍵,以便 ref 不在undefined事件處理程序內(nèi)keyDown?我見過很多涉及設(shè)置tabIndex和黑客攻擊以確保正確的元素在正確的時(shí)間獲得焦點(diǎn)的解決方案,但這些對我來說似乎并不是可靠的解決方案。我剛剛學(xué)習(xí) React,所以這可能是 React 的設(shè)計(jì)限制,或者我沒有正確設(shè)計(jì)我的組件。但這種功能對我來說似乎相當(dāng)基本,能夠?qū)⒔M件從一個(gè)組件傳遞到另一個(gè)組件并相互調(diào)用方法。
查看完整描述

1 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

您將調(diào)用 onKeyDown 回調(diào)兩次,一次在文檔上,一次在應(yīng)用程序上。事件在樹上冒泡。當(dāng)textarea沒有焦點(diǎn)時(shí),onlydocument.onkeydown被調(diào)用。當(dāng)它處于焦點(diǎn)時(shí),document.onkeydown和 Appdiv.onkeydown都會(huì)被調(diào)用,從而有效地取消效果(切換狀態(tài)關(guān)閉和重新打開)。

這是一個(gè)工作示例:https://codesandbox.io/s/icy-hooks-8zuy7 ?file=/src/App.js

import React from "react";


class Console extends React.Component {

? constructor(props) {

? ? super(props);

? ? this.state = {

? ? ? visible: false,

? ? ? text: ""

? ? };

? }


? print(output: string) {

? ? this.setState({

? ? ? text: this.state.text + output + "\n"

? ? });

? }


? toggleVisible() {

? ? this.setState({ visible: !this.state.visible });

? }


? render() {

? ? const footer_style = {

? ? ? display: this.state.visible ? "inline" : "none"

? ? };

? ? return (

? ? ? <footer

? ? ? ? id="console-footer"

? ? ? ? className="footer container-fluid fixed-bottom"

? ? ? ? style={footer_style}

? ? ? >

? ? ? ? <div className="row">

? ? ? ? ? <textarea id="console" className="form-control" rows={5} readOnly>

? ? ? ? ? ? {this.state.text}

? ? ? ? ? </textarea>

? ? ? ? </div>

? ? ? </footer>

? ? );

? }

}


export default class App extends React.Component {

? constructor(props) {

? ? super(props);

? ? this.console = React.createRef();

? }


? keyDown = (e) => {

? ? this.console.current.toggleVisible(); // <-- this is undefined

? };


? componentDidMount() {

? ? document.addEventListener("keydown", this.keyDown);

? }

? componentWillUnmount() {

? ? document.removeEventListener("keydown", this.keyDown);

? }


? render() {

? ? return (

? ? ? <div className="App" style={{ backgroundColor: "blueviolet" }}>

? ? ? ? enter key to toggle console

? ? ? ? <Console ref={this.console} />

? ? ? </div>

? ? );

? }

}

另外,我建議使用React 的 hooks:

export default App = () => {

? const console = React.createRef();


? const keyDown = (e) => {

? ? console.current.toggleVisible(); // <-- this is undefined

? };


? React.useEffect(() => {

? ? // bind onComponentDidMount

? ? document.addEventListener("keydown", keyDown);

? ? // unbind onComponentDidUnmount

? ? return () => document.removeEventListener("keydown", keyDown);

? });


? return (

? ? <div className="App" style={{ backgroundColor: "blueviolet" }}>

? ? ? press key to toggle console

? ? ? <Console ref={console} />

? ? </div>

? );

};


查看完整回答
反對 回復(fù) 2023-07-14
  • 1 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號