简体   繁体   English

有没有办法在 React 中定义一个可以在整个组件中使用的元素?

[英]Is there any way to define an element in React that can be used throughout the component?

For example I have the following component:例如,我有以下组件:

import React from 'react'

export default function App() {
  function handleOver() {
    const target = document.getElementById("target")
    target.style.background = "red"
  }

  function handleLeave() {
    const target = document.getElementById("target")
    target.style.background = "green"
  }
  
  return (
    <div>
      <div
        id="btn"
        onMouseOver={handleOver}
        onMouseLeave={handleLeave}>
          Test
      </div>
      <div
        id="target">
      </div>
    </div>
  )
}

Instead of defining target twice, can I define it once and call it from each event?除了定义目标两次,我可以定义一次并从每个事件中调用它吗?

You can useRef , but also check recommended approach in second code snippet .您可以useRef也可以在第二个代码片段中查看推荐的方法

import React from 'react'

export default function App() {
  const target = useRef();

  function handleOver() {
    target.current.style.background = "red"
  }

  function handleLeave() {
    target.current.style.background = "green"
  }
  
  return (
    <div>
      <div
        id="btn"
        onMouseOver={handleOver}
        onMouseLeave={handleLeave}>
          Test
      </div>
      <div
         ref={target}>
      </div>
    </div>
  )
}

But probably it's better not to manipulate the DOM like that yourself , instead define some backgroundColor state variable with useState and use style binding like <div style={{backgrondColor: myColorInState}}></div> or assign a custom CSS class with these colors: But probably it's better not to manipulate the DOM like that yourself , instead define some backgroundColor state variable with useState and use style binding like <div style={{backgrondColor: myColorInState}}></div> or assign a custom CSS class with these colors:

export default function App() {
  const [targetColor, setTargetColor] = useState(null);

  function handleOver() {
    setTargetColor("red");
  }

  function handleLeave() {
    setTargetColor("green");
  }
  
  return (
    <div>
      <div
        id="btn"
        onMouseOver={handleOver}
        onMouseLeave={handleLeave}>
          Test
      </div>
      <div style={{background: targetColor}}></div>
    </div>
  )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定义用作列表元素的组件 - Define a component used as list element 有什么办法可以在单击“无反应” dom元素时调用组件方法 - Is there any way to call a component method on clicking a “non react” dom element 在React中定义空dom元素的正确方法 - Correct way to define an empty dom element in React “组件”不能用作 JSX 组件。 它的元素类型&#39;ReactElement<any, any> | Component&lt;{}, any, any&gt;&#39; 不是有效的 JSX 元素 - 'Component' cannot be used as a JSX component. Its element type 'ReactElement<any, any> | Component<{}, any, any>' is not a valid JSX element 有没有办法在可重用组件中使用反应导航道具作为定义 object ? - Is there any ways to use react navigation props as a define object in reusable component? 有没有其他方法可以在 React 中为组件添加属性? - Is there any other way to add attributes to a component in React? 有没有办法从 React 组件中导出变量? - Is there any way to export a variable from an React component? vuejs组件中的@click不会有任何反应 - @click in vuejs component will not react in any way 有什么办法可以使此反应组件更简洁? - is there any way to make this react component less verbose? 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM