简体   繁体   English

React Hook “React.useEffect” 在 function “selectmenu” 中调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB5074F1477

[英]React Hook “React.useEffect” is called in function “selectmenu” which is neither a React function component or a custom React Hook function

Problem: React Hook "React.useEffect" is called in function "selectmenu" which is neither a React function component or a custom React Hook function.问题:React Hook "React.useEffect" 在 function "selectmenu" 中调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB4Z5074F。

Target: I want to Mount Component('component DidMount/WillUnmount) (using useEffect()) only when I click on a button, rather than, mounting while the file (or whole Component) is being loaded.目标:我只想在单击按钮时挂载 Component('component DidMount/WillUnmount) (使用 useEffect()),而不是在加载文件(或整个组件)时挂载。

Actual Goal: I want to select(or highlight) a file (custom ) on click.实际目标:我想在点击时选择(或突出显示)一个文件(自定义)。 But when the user clicks outside the dimensions of the file (), then the selected file should get deselected (remove highlight).但是,当用户在文件 () 的尺寸之外单击时,应取消选择所选文件(删除突出显示)。

export default function Academics() {
  let [ ismenuselected, setmenuselection] = useState(0)

  const selectmenu = () => {

    console.log("Menu to Select")

    React.useEffect(() => {
      console.log('Component DidMount/WillUnmount')

      return () => {
         console.log('Component Unmounted')
      }
    }, [isfolderselected]);
  }

  return (
          <div onClick={selectmenu}></div>
  )
}

Note:笔记:

  1. I've tried with the UPPER case of SelectFolder #56462812 .我已经尝试过 SelectFolder #56462812的大写。 Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
  2. I want to try something like this .我想尝试这样的事情。 But with a click of a button (useEffect() should invoke onClick event).但是单击一个按钮(useEffect() 应该调用 onClick 事件)。

I think I got what you're trying to accomplish.我想我得到了你想要完成的事情。 First, you can't define a hook inside a function.首先,您不能在 function 中定义挂钩。 What you can do is trigger the effect callback after at least one of its dependencies change.您可以做的是在至少其中一个依赖项发生更改后触发效果回调。

useEffect(() => {
  // run code whenever deps change
}, [deps])

Though for this particular problem (from what I understood from your description), I'd go something like this:虽然对于这个特殊问题(根据我从你的描述中了解到的),我会 go 是这样的:

export default function Academics() {
  let [currentOption, setCurrentOption] = useState()

  function handleSelectOption(i) {
    return () => setCurrentOption(i)
  }

  function clearSelectedOption() {
    return setCurrentOption()
  }

  return (options.map((option, i) =>
    <button
      onClick={handleSelectOption(i)}
      className={i === currentOption ? 'option option--highlight' : 'option'}
      onBlur={clearSelectedOption}
    ></button>
  ))
}

暂无
暂无

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

相关问题 React Hook &quot;useEffect&quot; 在函数 &quot;shoes&quot; 中调用,它既不是 React 函数组件,也不是自定义 React Hook - React Hook "useEffect" is called in function "shoes" which is neither a React function component or a custom React Hook React Hook &quot;useState&quot; 在函数 &quot;setResults&quot; 中调用,它既不是 React 函数组件,也不是自定义 React Hook 函数 - React Hook "useState" is called in function "setResults" which is neither a React function component or a custom React Hook function React Hook “useReducer” 在 function “fetchData” 中被调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB5074C17A9F - React Hook “useReducer” is called in function “fetchData” which is neither a React function component or a custom React Hook function React Hook 在 function “onSubmit” 中调用,它既不是 React function 组件也不是自定义 React Hook function - React Hook is called in function “onSubmit” which is neither a React function component or a custom React Hook function React Hook &quot;useContext&quot; 在函数 &quot;age&quot; 中调用,它既不是 React 函数组件,也不是自定义的 React Hook 函数 - React Hook "useContext" is called in function "age" which is neither a React function component or a custom React Hook function React Hook &quot;useStyles&quot; 在函数 &quot;appBar&quot; 中调用,它既不是 React 函数组件,也不是自定义 React Hook 函数 - React Hook "useStyles" is called in function "appBar" which is neither a React function component or a custom React Hook function React Hook “useState” 或 React Hook “useEffect” 在 function 中调用,它既不是 React function - React Hook “useState” or React Hook “useEffect” is called in function which is neither a React function React Hook“useState”在 function“increaseCounter”中被调用,它既不是 React function 组件,也不是自定义 React Hook function - React Hook "useState" is called in function "increaseCounter" that is neither a React function component nor a custom React Hook function 在既不是 React 函数组件也不是自定义 React Hook 函数的函数中调用 React Hook “useAxios” - React Hook "useAxios" is called in function that is neither a React function component nor a custom React Hook function React Hook "useDispatch" 在 function "requestWithAction" 中被调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E683854F14ZA7 - React Hook "useDispatch" is called in function "requestWithAction" that is neither a React function component nor a custom React Hook function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM