weimo383
V2EX  ›  问与答

问一个 react reusable 组件的的问题

  •  
  •   weimo383 · Aug 11, 2020 · 1889 views
    This topic created in 2276 days ago, the information mentioned may be changed or developed.
    
    import React, { Fragment} from 'react';
    
    const App = () => {
    
      return (
        <div>
          <h1>My App</h1>
    
          <InputWithLabel
            id="search"
            label="Search"
            value={"1"}
          />
          <InputWithLabel
            id="search2"
            label="Search2"
            value={"2"}
          />
          <hr />
        </div>
      );
    };
    
    const InputWithLabel = ({
      id,
      label,
      value,
      type = 'text',
      onInputChange,
    }) => (
        <Fragment>
          <label htmlFor={id}>{label}</label>
    
          <input
            id={id}
            type={type}
            value={value}
            onChange={onInputChange}
            autoFocus
          />
        </Fragment>
      );
    
    export default App;
    
    

    当我在 App 中 render 两个该组件时,发现 autoFocus 只有最后一个组件接收到了。为什么???

    johnnyNg
    johnnyNg
    Aug 11, 2020❤️1
    1
    你见过页面里面同时两个输入框 focus 的吗?
    susan0007
    susan0007
    Aug 11, 2020
    2
    哈哈哈哈哈哈哈哈哈哈哈哈哈
    zhao7399686
    zhao7399686
    Aug 11, 2020
    3
    哈哈哈哈哈哈哈哈哈哈哈哈哈
    icebay
    icebay
    Aug 11, 2020
    4
    哈哈哈哈哈哈哈哈哈哈哈哈哈
    qiutianaimeili
    qiutianaimeili
    Aug 11, 2020
    5
    1 楼完美答案
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2796 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 14:10 · PVG 22:10 · LAX 06:10 · JFK 09:10
    ♥ Do have faith in what you're doing.