亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 React 中,如何從不同組件的事件處理程序觸發自定義按鈕的點擊事件?

在 React 中,如何從不同組件的事件處理程序觸發自定義按鈕的點擊事件?

函數式編程 2021-11-18 20:29:48
我有一個 React 應用程序,它的界面使用Material UI。我創建了一個自定義按鈕組件,它為默認的 Material UI 按鈕設置樣式并使用 redux。render()我的按鈕組件的功能如下所示:        return (            <div className={classes.buttonWrapper}>                <Button                     ref={this.props.innerRef}                    disabled={loading || disabled}                    onClick={this.handleClick}                    {...other}>                    <React.Fragment>                        {children}                        {this.buildLoader(loading, classes)}                    </React.Fragment>                </Button>            </div>        );我想要的是能夠在頁面上包含這個按鈕,并讓 UI 通過點擊以外的其他方式觸發它的點擊事件。例如,在登錄表單上,我希望當前關注密碼文本框的用戶能夠通過按 Return/Enter 鍵觸發按鈕單擊。我確定我需要在 React 中使用轉發引用的概念,但我對 React 還很陌生,無法讓它工作。你可以在我的按鈕上看到我已經定義了一個ref集合this.props.innerRef。我的按鈕組件(稱為WaitingButton)是這樣導出的:const withInnerRef = React.forwardRef((props, ref) => <WaitingButton   innerRef={ref} {...props}/>);var component = withStyles(styles)(withInnerRef);export default connect(mapStateToProps, mapDispatchToProps)(component);然后我將此按鈕添加到這樣的表單中:    <Paper>        <TextField             style={{marginBottom: '8px'}}            label="A textbox!"            fullWidth             onKeyPress={(e) => { if (e.key === "Enter") this.triggerClick(); }} />        <WaitingButton             ref={this.submitButton}            variant="contained"            color="primary"            onClick={(e) => {                console.log('Button clicked :)', e.target);            }}>            Press enter in textbox!        </WaitingButton>    </Paper>請參閱我已分配按鈕,ref并在此頁面的構造函數中使用this.submitButton = React.createRef();最后triggerClick看起來像這樣:    triggerClick() {        console.log('CLICK', this.submitButton.current);        this.submitButton.current.click();    }當我在文本框中按 Enter 鍵時,我可以檢查分配給的值,this.submitButton.current并可以看到它是connect我包裹按鈕的 Redux對象。但是,我也得到了錯誤this.submitButton.current.click is not a function,很明顯,ref 沒有一直轉發到按鈕本身。恐怕我有點失落,所以呼吁您的幫助!
查看完整描述

2 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

只是想確保,您想要的是:當用戶Enter在文本字段上鍵入時按下時,按鈕將顯示加載視覺效果,對嗎?我認為您不必將 ref 傳遞給按鈕組件,您可以將狀態傳遞isLoadingShown到您的WaitingButton


等待按鈕.js


 return (

        <div className={classes.buttonWrapper}>

            <Button 

                ref={this.props.innerRef}

                disabled={loading || disabled}

                onClick={this.handleClick}

                {...other}>

                <React.Fragment>

                    {children}

                    {this.props.isLoadingShown && this.buildLoader(loading, classes)}

                </React.Fragment>

            </Button>

        </div>

    );

然后在表單組件中


state = {

   isLoadingShown: false,

}


triggerClick() {

    this.setState({ isLoadingShown: true })

}


render(){

   ...

   <Paper>

       <TextField 

           style={{marginBottom: '8px'}}

           label="A textbox!"

           fullWidth 

           onKeyPress={(e) => { if (e.key === "Enter") this.triggerClick(); }} />

       <WaitingButton

           variant="contained"

           color="primary"

           isLoadingShown={this.state.isLoadingShown}

           onClick={(e) => {

               console.log('Button clicked :)', e.target);

           }}>

           Press enter in textbox!

       </WaitingButton>

   </Paper>

   ...

}

不要忘記isLoadingShown再次設置為falsecomponentWillUnmount


查看完整回答
反對 回復 2021-11-18
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我只是試圖重現你的情況。我為它創建了一個代碼沙盒。我想我發現了問題??雌饋?React.forwardRef 只適用于 prop name forwardedRef 所以嘗試在你的代碼中將 innerRef 屬性重命名為 forwardedRef 。


const withInnerRef = React.forwardRef((props, ref) => <WaitingButton 

  forwardedRef={ref} {...props}

/>);

還有你的 render() 函數


<Button 

    ref={this.props.forwardedRef}

    disabled={loading || disabled}

    onClick={this.handleClick}

    ...

你可以用我的簡化代碼和盒子試試https://codesandbox.io/s/intelligent-cori-rb5ce


查看完整回答
反對 回復 2021-11-18
  • 2 回答
  • 0 關注
  • 483 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號