简体   繁体   English

如何更改按钮名称?(Javascript)

[英]How can I change the button name?(Javascript)

(This article was written using Google Translate.) (这篇文章是用谷歌翻译写的。)

This is App.js.这是 App.js。

import React,{useState} from 'react';
import Counter from './components/Counter'

function App() {
  return (
    <div className="App">
      <h1>Code</h1>
      <Counter />
      <Counter />
      <Counter />
    </div>
  );
}
export default App;

This is Counter.js这是 Counter.js

import React, {useState} from 'react'

const Counter = () => {
    const [count, setCount] = useState(0)
  
  const increment = () => {
    setCount(count + 1)
  }
  return (
      <button onClick={increment}>
        Click {count}
        </button>
  );
}

export default Counter;

This is a chrome screen.这是一个镀铬屏幕。 Each number is incremented by 1 when the button is pressed.按下按钮时,每个数字都加 1。 在此处输入图像描述

But that's not what's important.但这不是最重要的。

I want the first button to be named 'click a'.我希望将第一个按钮命名为“单击 a”。 And I want to name the second button 'click b'.我想将第二个按钮命名为“单击 b”。 And I want to name the third button 'click c'.我想将第三个按钮命名为“单击 c”。

I'm learning react for the first time today, so I'm still bad at it.我今天第一次学习反应,所以我还是很糟糕。 I'd appreciate it if you could tell me how.如果你能告诉我怎么做,我将不胜感激。

If you want to set a,b,c or any other name to your button name use props to pass the value you want to your button如果您想设置 a,b,c 或任何其他名称到您的按钮名称,请使用道具将您想要的值传递给您的按钮

import React, {useState} from 'react'

const Counter = (props) => {
    const { buttonTitle } = props;
    const [count, setCount] = useState(0)
  
  const increment = () => {
    setCount(count + 1)
  }
  return (
     <>
      <h2>{count}</h2>
      <button onClick={increment}>
        Click {buttonTitle}
        </button>
     </>
  );
}
export default Counter;

and

import React,{useState} from 'react';
import Counter from './components/Counter'

function App() {
  return (
    <div className="App">
      <h1>Code</h1>
      <Counter buttonTitle="a" />
      <Counter buttonTitle="b" />
      <Counter buttonTitle="c" />
    </div>
  );
}
export default App;

In your Counter component you can accept props from App Component在您的Counter组件中,您可以接受来自App Component 的道具

import React, {useState} from 'react'

const Counter = (props) => {
    const [count, setCount] = useState(0)
  
  const increment = () => {
    setCount(count + 1)
  }
  return (
      <button onClick={increment}>
        Click {props.name}
        </button>
  );
}

export default Counter;

From your App component you can pass down name prop to Counter Component从您的App组件中,您可以将 name prop 传递给Counter组件

import React,{useState} from 'react';
import Counter from './components/Counter'

function App() {
  return (
    <div className="App">
      <h1>Code</h1>
      <Counter name="a" />
      <Counter name="b" />
      <Counter name="c" />
    </div>
  );
}
export default App;
import React,{useState} from 'react';
import Counter from './components/Counter'

function App() {
  return (
    <div className="App">
      <h1>Code</h1>
      <Counter name='a'/>
      <Counter name='b'/>
      <Counter name='c'/>
    </div>
  );
}
export default App;

//counter.js
import React, {useState} from 'react'

const Counter = (props) => {
    const [count, setCount] = useState(0)
  
  const increment = () => {
    setCount(count + 1)
  }
  return (
      <button onClick={increment}>
        Click {props.name}
        </button>
<p>{count}<p/>
  );
}

export default Counter;

I think you should try like this Click {String.fromCharCode(count+97)}我认为你应该尝试这样Click {String.fromCharCode(count+97)}

String.fromCharCode(number) it converts number to character & +97 because small alphabets in ASCII starts from 97, for reference check ASCII table. String.fromCharCode(number) 它将数字转换为字符 & +97,因为 ASCII 中的小字母从 97 开始,以供参考检查 ASCII 表。 I hope you wanted this answer.我希望你想要这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM