簡體   English   中英

輸入字段在React中失去焦點

[英]Input field looses focus in React

我知道發生這種情況是因為React正在重新渲染頁面,因此輸入字段失去了焦點。 我已經嘗試了所有我能想到的東西,但是沒有一個起作用! 我找不到解決此問題的方法。 這是我的React代碼。

import React, { ChangeEvent, Component, ReactNode } from 'react'
import { Root, /*Routes*/ } from 'react-static'
import { Link } from '@reach/router'
import { Helmet } from 'react-helmet'
import './app.css'
// import FancyDiv from '@components/FancyDiv'

/* According to the following documentation,
 * https://github.com/nozzle/react-static/blob/master/docs/concepts.md#writing-universal-node-safe-code
 */
let Sidenav = { init(_: any, options: any) { return {_, options} } }
let updateTextFields = () => {}
if (typeof window !== 'undefined') {
  const materialize = require('materialize-css')
  Sidenav = materialize.Sidenav
  updateTextFields = () => materialize.updateTextFields()
}

interface IComponentProps {

}

interface IComponentState {
  'long_url': string
}

const LongUrlInput = (props: any) =>
  <input
    key='in1'
    id="long_url"
    type="text"
    className="validate"
    onChange={ props.handleLongUrl }
  />

export default class App extends Component<IComponentProps, IComponentState> {
  constructor(props: any) {
    super(props)
    this.state = {
      'long_url': ''
    }
  }

  componentDidMount(): void {
    const elem = document.querySelectorAll('.sidenav')
    Sidenav.init(elem, {})
    updateTextFields()
  }

  handleLongUrl = (event: ChangeEvent<HTMLInputElement>): void => {
    console.log('types', event.target.value, this.state.long_url)
    this.setState({
      'long_url': event.target.value
    })
  }

  render(): ReactNode {
    return (
      <Root>

        <Helmet>
          <title>ShortURI - URL Shortener</title>
          <meta name="description" content="Create short URLs and also monitor traffic with proper analysis report." />
        </Helmet>

        <nav className={'indigo'}>
          <div className="nav-wrapper" style={{ padding: '0 20px' }}>
            <Link to={'/'} className={'brand-logo'}>ShortURI</Link>
            <a href={''} data-target="slide-out" className="sidenav-trigger">
              <i className="material-icons">menu</i>
            </a>
            <ul id="nav-mobile" className="right hide-on-med-and-down">
              <li><Link to={'/about'}>About</Link></li>
              <li><Link to={'/#!'}>Login</Link></li>
              <li><Link to={'/#!'}>Register</Link></li>
            </ul>
          </div>
        </nav>
        <ul id="slide-out" className="sidenav">
          <li><a className="sidenav-close" href="#">Clicking this will close Sidenav</a></li>
        </ul>

        <div className="content">
          <div className="row">
            <div className="card">
              <div className="card-content black-text">
                <span className="card-title">Card Title</span>
                <div className="input-field col s6">
                  <LongUrlInput handleLongUrl={this.handleLongUrl}/>
                  <label className="active" htmlFor="long_url">Long URL</label>
                </div>
              </div>
            </div>
          </div>
          {/*<FancyDiv>*/}
            {/*<Routes/>*/}
          {/*</FancyDiv>*/}
        </div>
      </Root>
    )
  }
}

PS:我是React的新手。 任何幫助,將不勝感激。

注意:鍵入一些隨機單詞以抑制StackOverflow警告-“看起來您的問題主要是代碼,等等。”

我將<Root>...</Root>更改為<div>...</div>並且現在可以正常工作了。 奇怪的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM