如何在React中创建角色计数器?

发布于 2025-02-13 03:11:41 字数 595 浏览 0 评论 0原文

我是新手,第一次与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋叶绚丽 2025-02-20 03:11:42

这是带有React和Usestate钩的Textarea的示例。
Usestate返回一个状态的Getter和Setter。
文本值存储在文本中,并使用SetState更新。
Textarea分别使用valueonChange参数消耗状态。

import './App.css';
import { useState } from 'react';

function App() {
  const MAX_TEXT_LENGTH = 800;
  const [text, setText] = useState("");

  function handleTextAreaChange(event) {
    const value = event.target.value;
    if (value.length <= MAX_TEXT_LENGTH) {
      setText(value);
    }
  }
  return (
    <div className="App">
      <div>
        {`${text.length}/${MAX_TEXT_LENGTH}`}
      </div>
      <textarea rows={16} cols={64}
        onChange={handleTextAreaChange}
        value={text}>
      </textarea>
    </div>
  );
}

export default App;

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 with setState.
textarea consume the state in handler with value and onChange parameter respectively.

import './App.css';
import { useState } from 'react';

function App() {
  const MAX_TEXT_LENGTH = 800;
  const [text, setText] = useState("");

  function handleTextAreaChange(event) {
    const value = event.target.value;
    if (value.length <= MAX_TEXT_LENGTH) {
      setText(value);
    }
  }
  return (
    <div className="App">
      <div>
        {`${text.length}/${MAX_TEXT_LENGTH}`}
      </div>
      <textarea rows={16} cols={64}
        onChange={handleTextAreaChange}
        value={text}>
      </textarea>
    </div>
  );
}

export default App;

风启觞 2025-02-20 03:11:42

欢迎反应:)
这不是应该的方式,如果您想创建一个与计数器的组件,那应该是这样的:

function Counter() {
const [count, setCount] = useState(0);

return (
    <div>
        {/*This will increment count by 1*/}
        <input type={"button"} value={"Click me"} onClick={() => setCount(count + 1)} />
        {/*This will decrement the count by 1*/}
        <input type={"button"} value={"Click me"} onClick={() => setCount(count - 1)} />
        {/*This will display the count*/}
        {count}
    </div>


)}

与之一起播放,但这就是您在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:

function Counter() {
const [count, setCount] = useState(0);

return (
    <div>
        {/*This will increment count by 1*/}
        <input type={"button"} value={"Click me"} onClick={() => setCount(count + 1)} />
        {/*This will decrement the count by 1*/}
        <input type={"button"} value={"Click me"} onClick={() => setCount(count - 1)} />
        {/*This will display the count*/}
        {count}
    </div>


)}

Just play along with it, but this is how you update the dom in React

神也荒唐 2025-02-20 03:11:42

反应中的事件通过渲染的JSX中的 OneVent 属性处理。
https://reaectjs.org/docs/handling-handling-events.html
maxlength属性指定&lt; input&gt;&amp; amp;的最大计数。 &lt; textarea&gt;
https://developer.mozilla.org/en-us/docs/web/html/element/textarea#attr-maxlength

class Counter extends React.Component {

    constructor(props) {
        super(props);
        this.state = {text: ''};
    }

    handleTextChange=(evt)=> {
        this.setState({ text: evt.target.value });
    }

    render() {
        return <div className="App">
            <textarea value={this.state.text} onChange={this.handleTextChange} maxLength="800" ></textarea>
            <p>count: {this.state.text.length}</p>
        </div>
    }
}

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

class Counter extends React.Component {

    constructor(props) {
        super(props);
        this.state = {text: ''};
    }

    handleTextChange=(evt)=> {
        this.setState({ text: evt.target.value });
    }

    render() {
        return <div className="App">
            <textarea value={this.state.text} onChange={this.handleTextChange} maxLength="800" ></textarea>
            <p>count: {this.state.text.length}</p>
        </div>
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文