Confusing for me question is: as a convention i give a name for arrow function components in react by starting from lowercase letter:
const todo = () => {
return (
<div>
</div>
)
}
export default todo
Then, i'm trying to use 'useState' Hook, and have the error:
Failed to compile.
./src/components/Todo.js
Line 4: React Hook "useState" is called in function "todo" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
For success compile i need to rename component as 'Todo'
In App.js i'm using it like:
import Todo from './components/Todo'
And this is ok, but when i'm using hook i'm getting error. What is wrong with naming?
UPD
Full code of component. I want to rename it const todo, not const Todo. Why i'm getting error?
import React, {useState} from 'react'
const Todo = props => {
const [todoName, setTodoName] = useState('')
const [todoList, setTodoList] = useState([])
// const [todoState, setTodoState] = useState({userInput: '', todoList: []})
const inputChangeHandler = (event) => {
setTodoName(event.target.value)
// setTodoState({
// userInput: event.target.value,
// todoList: todoState.todoList
// })
}
const todoAddHandler = () => {
setTodoList(todoList.concat(todoName))
// setTodoState({userInput: todoState.userInput, todoList: todoState.todoList.contact(todoState.userInput)})
}
return (
<div>
<input type='text' placeholder='Todo' onChange={inputChangeHandler} value={todoName} />
<button onClick={todoAddHandler}>Add todo</button>
<ul>
{todoList.map(todo => (
<li key={todo}>{todo}</li>
))}
</ul>
</div>
)
}
export default Todo