如何在React中创建角色计数器?
我是新手,第一次与React创造了一些东西。我想在react中的文本中创建一个角色计数器。该计数器的最大长度应为800个字符。我创建了此代码,但是我不知道如何在不收到错误消息的情况下包含const。
import React from 'react'
import { Component } from 'react';
function Counter() {
return (
const rezensionTextArea = document.getElementById('rezension_textarea');
const zeichenCounter = document.getElementById('zeichen_counter');
rezensionTextArea.addEventListener('input', () => {
const zeichen = rezensionTextArea.value.length;
zeichenCounter.textContent = `${zeichen}/800`;
});
)
}
export default Counter
I am new and creating something with react for the first time. i want to create a character counter in a textarea in react. The counter should have a maximum length of 800 characters. i created this code, but i don't know how to include const without getting an error message.
import React from 'react'
import { Component } from 'react';
function Counter() {
return (
const rezensionTextArea = document.getElementById('rezension_textarea');
const zeichenCounter = document.getElementById('zeichen_counter');
rezensionTextArea.addEventListener('input', () => {
const zeichen = rezensionTextArea.value.length;
zeichenCounter.textContent = `${zeichen}/800`;
});
)
}
export default Counter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是带有React和Usestate钩的Textarea的示例。
Usestate返回一个状态的Getter和Setter。
文本值存储在
文本
中,并使用SetState
更新。Textarea分别使用
value
和onChange
参数消耗状态。Here is an example of textarea with react and useState hook.
useState return an array of getter and setter of a state.
the text value is stored in
text
and updated withsetState
.textarea consume the state in handler with
value
andonChange
parameter respectively.欢迎反应:)
这不是应该的方式,如果您想创建一个与计数器的组件,那应该是这样的:
与之一起播放,但这就是您在React中更新DOM的方式
welcome to react :)
That's not how it supposed to be, if you want to create a component with counter it should look like this:
Just play along with it, but this is how you update the dom in React
反应中的事件通过渲染的JSX中的 OneVent 属性处理。
https://reaectjs.org/docs/handling-handling-events.html
maxlength
属性指定< input>
& amp;的最大计数。< textarea>
。https://developer.mozilla.org/en-us/docs/web/html/element/textarea#attr-maxlength
Events in React are handled through the onEvent properties in the rendered JSX.
https://reactjs.org/docs/handling-events.html
The
maxLength
attribute specifies the maximum count of a<input>
&<textarea>
.https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea#attr-maxlength