在React-Js中选择和取消选择多个DIV
在react-js单击一个Div中,DIV将突出显示并再次单击它将正常,我们选择多个DIV并取消选择,选择最多四个Div仅这些是条件。 我在React-js的初学者是我的div,
<div className='todoDiv select'>Task1</div>
<div className='todoDiv'>Task2</div>
<div className='todoDiv'>Task3</div>
<div className='todoDiv'>Task4</div>
<div className='todoDiv'>Task5</div>
这里选择 class用于突出显示DIV,如何解决这个问题,我在React-js中拥有基本知识,请帮助我
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将通过 usestate(usestate(usestate() ,并维护每个待办事项属性的属性。当单击戒酒时,所选属性会更改。
要将选择限制为4,只需添加一个与以下类似计数的支票。
I'd approach this by storing an array of todo objects in state with
useState()
, and maintain aselected
property for each todo. As the todos are clicked, the selected property is changed.To limit the selections to 4, simply add a check with a count like below.
CodeSandbox demo
在ReactJ中,我们使用状态跟踪和呈现组件的视图。在您的情况下,我们可以创建一个称为“选择的状态”,该状态可维护所选任务的列表。
最初,它是空的。当用户单击任务时,将其添加到选定任务的列表中。在更新中,我们实现了所需的逻辑,如上所示。在每个任务的className中注意我们使用Javescript模板字符串。如果用户选择了类名,这有助于我们将“选定”类“选择”添加到类名称。
In Reactjs, we use state to keep track of and render the view of the component. In your case, we can create a state called selected which maintains the list of selected tasks.
Initally, it is empty. When the user clicks on a task, it is added to the list of selected tasks. In the updateSelected, we implement required logic as shown above. Notice in the className for each task we use JaveScript template strings. This helps us to conditionally add class 'selected' to the className if it is selected by the user.
我添加了
task_list
以动态渲染Div。这里toggletask
函数将选择&amp;去选择Div。正如您所说,最多将选择四个Div。I have added a
TASK_LIST
to dynamically render the div. HeretoggleTask
function will both select & de-select the div. And as you said, maximum of four div will be selected.一个完整的组件示例:
A complete component example:
您可以将样式放入类似的变量中,
然后将其放在Div中,
然后将其放在Div上,在DIV上创建一个更改“样式”变量的函数。 样式变量的状态
更改您获得JIST的
you can put styles in a variable like so
Then put this in your div like so
Then, create a function onClick on the div that changes the "style" variable. Change the state of the the style variable
You get the jist