[英]How do i target React component which is clicked
I want to change the class of a component when clicked.I use state to create a new component with the properties of name and done(which is upon creation false).Then this is pushed into the todos array.我想在单击时更改组件的 class。我使用 state 创建一个具有 name 和 done 属性的新组件(在创建时为 false)。然后将其推送到 todos 数组中。 Now the question is how do I find which component is clicked and change its "done" property to?done ?
现在的问题是如何找到单击了哪个组件并将其“完成”属性更改为?完成?
function App() {
const [todos, setTodos] = useState([])
const [newTodo, setNewTodo] = useState({
name: "",
done: false
})
const handleInput = (event) => {
setNewTodo({name: event.target.value})
}
const handleDone = (event) => {
//WHAT TO DO HERE
}
const submitTodo = (event) => {
event.preventDefault();
setTodos([...todos, newTodo.name])
console.log(newTodo.name)
setNewTodo({name: ""})
}
return (
<div className="App">
<form onSubmit={submitTodo}>
<input onChange={handleInput} value={newTodo.name}/>
<button>Add Todo!</button>
</form>
<ul>
{todos.map(todo => (
<li className={/*Change the class based on the DONE property*/} onClick={handleDone}>{todo}</li>
))}
</ul>
</div>
);
}
export default App;
change your newTodo state更改您的新Todo state
const [newTodo, setNewTodo] = useState('');
const [show,setShow] = useState(false);
const handleDone = (event) => setShow(!show)
.....
.....
.....
<li className={show ? 'classname when show is true': 'classname when show is false'} onClick={handleDone}>{todo}</li>
First of all, you should consistently make your todos contain both name
and done
properties.首先,您应该始终如一地让您的待办事项同时包含
name
和done
属性。 Having this you just pass the todo from the clicked <li>
to your click handler so it can modify its done property and cause rerender by updating todos
state.有了这个,您只需将点击的
<li>
中的 todo 传递给您的点击处理程序,以便它可以修改其 done 属性并通过更新todos
state 来导致重新渲染。
function App() {
const [todos, setTodos] = useState([])
const [newTodo, setNewTodo] = useState({
name: "",
done: false
})
const handleInput = (event) => {
setNewTodo({name: event.target.value, done: false }); // this now adds done property
}
const handleDone = (todo) => {
todo.done = !todo.done;
setTodos([...todos]); // This sets new todos to cause a rerender with updated list
}
const submitTodo = (event) => {
event.preventDefault();
setTodos([...todos, newTodo]) // this now appends entire newTodo item not only it's name
setNewTodo({name: "", done: false}) // this now also sets default done
}
return (
<div className="App">
<form onSubmit={submitTodo}>
<input onChange={handleInput} value={newTodo.name}/>
<button>Add Todo!</button>
</form>
<ul>
{todos.map(todo => (
<li
className={todo.done ? 'classA' : 'classB'}
onClick={() => handleDone(todo)}
>
{todo.name} // this now displays todo.name not todo
</li>
))}
</ul>
</div>
);
}
export default App;
Additionally you could extract todos and the click handler into a separate component:此外,您可以将待办事项和点击处理程序提取到单独的组件中:
function ToDo({ item }) {
const [clicked, setClicked] = useState(item.done);
const handleClick = useCallback(() => {
item.done = !clicked
setClicked(!clicked);
}, [clicked]);
return <li
className={clicked ? 'classA' : 'classB'}
onClick={handleClick}
>
{item.name}
</li>;
}
Then just render a list of components with your App component:然后只需使用您的 App 组件呈现组件列表:
<ul>
{todos.map(todo => <ToDo item={todo} />
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.