【问题标题】:Change text value on button click更改按钮单击时的文本值
【发布时间】:2022-01-08 08:41:01
【问题描述】:

我是 REACT JS 的新手,所以在制作应用程序时,我被困在这部分,我想在单击按钮时更改 h1 值

import Button from 'react-bootstrap/Button';
import './App.css';
import { Container } from 'react-bootstrap';
import { Row } from 'react-bootstrap';

function App() {

return (
<Container>
    
<Row className="Row">
  <div className="frame">
 
    <h1>this text should change</h1>
       <Button className=" btn btn1" color="light">YES</Button> // this button should change the text
       <Button className=" btn btn2" color="light">NO</Button>
  </div>
</Row>

</Container>
);
}

export default App;

【问题讨论】:

标签: javascript reactjs button onclick


【解决方案1】:

在这种情况下,你必须使用钩子state (Documentation)。

所以,首先你必须导入 useState 钩子,然后创建它并最终使用它。

所以,你的代码会是这样的:

import Button from 'react-bootstrap/Button';
import './App.css';
import { Container } from 'react-bootstrap';
import { Row } from 'react-bootstrap';
import { useState } from 'react';

function App() {

  const [text, setText] = useState("this text should change");

  return (
    <Container>
    
    <Row className="Row">
      <div className="frame">
 
      <h1>{text}</h1>
        <Button className=" btn btn1" color="light" onClick={e => setText("new text")}>YES</Button> // this button should change the text
        <Button className=" btn btn2" color="light">NO</Button>
     </div>
    </Row>

   </Container>
  );
}

export default App;

【讨论】:

  • 嗨,我觉得onClick={setText("new text"} - 这可能会引发错误或可能无法正常工作。或者文本已经被设置为新文本,因为它需要一个函数,但是你正在执行并返回一些新的东西。
  • 是的,你是对的。我编辑了它
【解决方案2】:

永远不要访问真实的 DOM。使用状态并以 React 方式呈现。

我通常会使用状态来更改某些内容 - 请参阅 Using the State Hook。然后渲染出来。创建这个:

const [Text, setText] = useState("this text should change");

渲染它:

<h1>{Text}</h1>

使用事件处理程序并使用type="button",这样它就不会刷新页面:

  <Button
    className=" btn btn1"
    color="light"
    type="button"
    onClick={() => {
      setText("Praveen is awesome! You clicked on YES button!");
    }}
  >
    YES
  </Button>
  {/*// this button should change the text*/}
  <Button
    className=" btn btn2"
    color="light"
    type="button"
    onClick={() => {
      setText("Praveen is awesome! You clicked on NO button!");
    }}
  >
    NO
  </Button>

现在看看魔法:https://557w4.csb.app/

代码:

import Button from "react-bootstrap/Button";
import "./App.css";
import { Container } from "react-bootstrap";
import { Row } from "react-bootstrap";
import { useState } from "react";

function App() {
  const [Text, setText] = useState("this text should change");
  return (
    <Container>
      <Row className="Row">
        <div className="frame">
          <h1>{Text}</h1>
          <Button
            className=" btn btn1"
            color="light"
            type="button"
            onClick={() => {
              setText("Praveen is awesome! You clicked on YES button!");
            }}
          >
            YES
          </Button>
          {/*// this button should change the text*/}
          <Button
            className=" btn btn2"
            color="light"
            type="button"
            onClick={() => {
              setText("Praveen is awesome! You clicked on NO button!");
            }}
          >
            NO
          </Button>
        </div>
      </Row>
    </Container>
  );
}

export default App;

预览

完整代码沙盒:https://codesandbox.io/s/broken-snow-557w4?file=/src/App.js

【讨论】:

  • e.preventDefault(); ?只需将其设为type="button"
  • @RokoC.Buljan 我通常将其用于安全目的,是否需要避免?当然,感谢您的提醒。
  • @RokoC.Buljan 已更新。
  • button 应始终设置为 type="button" 否则默认为 type="submit" - 这显然不是您想要的。而Event.preventDefault() 仅适用于按钮类型为提交的情况。
  • @RokoC.Buljan 谢谢。已更新。
猜你喜欢
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 2020-11-17
相关资源
最近更新 更多