无效的钩子调用。钩子只能在函数组件的主体内部调用。当我从 material-ui

时间:2021-03-19 06:40:45

标签: javascript reactjs dom react-hooks

当我从 material-ui 导入按钮类时发生此错误。

在我的 App.js 文件中,当我添加 Button 标签并从 material-ui 导入 '@material-ui/core' 时,它给了我一个错误,即无效的钩子调用。钩子只能在函数组件的主体内部调用。所以我现在做什么我不知道。我是 react-js 的新手。请帮我解决这个问题...

import React,{useState} from 'react';
import { Button } from '@material-ui/core';
import './App.css';

function App() {

        const [todos,setTodos] = useState(['take dogs for a walk','take the rubbish out','help them who cant help back']);
        const [input, setInput] = useState('');
        console.log('hassam:',input);
        const addTodo= (event)=>  {
          //fire off when you click Add Todo button...
          event.preventDefault(); //will stop the refreshing...
          console.log('it got tapped'); 
          // setTodos([...todos,'here is the new to do'])
          setTodos([...todos,input])
          setInput('');  //its clearing the input field...
        }
        
        
  return (
    <div className="App">
      <form>
      <input value={input} onChange={event=>setInput(event.target.value)}/>
      
      <Button type='submit' onClick={addTodo} variant="contained" color="primary">
          Add Todo
          </Button>
      
      </form>
     
      <ul>
        {todos.map(todo => (<li>{todo}</li>))}

      </ul>
    </div>
  );
}

export default App;

0 个答案:

没有答案