简体   繁体   English

如何使用 PrimeReact 的 Button 组件?

[英]How to use PrimeReact's Button component?

I am trying out the javascript map method whilst building a simple calculator.我正在尝试 javascript map 方法,同时构建一个简单的计算器。

I am using a list for each row of the calculator and mapping it to a table cell and a prime react button inside it.我正在为计算器的每一行使用一个列表,并将其映射到表格单元格和其中的主要反应按钮。

I am however getting weird results in the browser.然而,我在浏览器中得到了奇怪的结果。

My code in the component is below and I have screenshotted the output.我在组件中的代码如下,我已经截取了输出。 enter image description here在此处输入图片说明

 import React, { Component } from 'react' import {Button} from 'primereact/button' class Calculator extends Component { constructor(props) { super(props); this.state = { } } render() { const values1 = ["7", "8", "9", "/"] const values2 = ["4", "5", "6", "x"] const values3 = ["1", "2", "3", "-"] const values4 = ["0", ".", "+", "="] const valuesList1 = values1.map(value => <td><Button>{value}</Button></td>) const valuesList2 = values2.map(value => <td><Button>{value}</Button></td>) const valuesList3 = values3.map(value => <td><Button>{value}</Button></td>) const valuesList4 = values4.map(value => <td><Button>{value}</Button></td>) return ( <div className="Middle"> <section className="InputContainer"> <table className="Keys"> <tbody> <tr> {valuesList1} </tr> <tr> {valuesList2} </tr> <tr> {valuesList3} </tr> <tr> {valuesList4} </tr> </tbody> </table> </section> </div> ); } } export default Calculator;

It seems the problem may be coming from the <Button/> component as it renders additional text that seems to be its className within it.似乎问题可能来自<Button/>组件,因为它呈现了似乎是其中的className附加文本。

I see you are using the PrimeReact library for the button and according to their documentation , you should enter the button's text via the label prop.我看到您正在为按钮使用 PrimeReact 库,根据他们的文档,您应该通过label道具输入按钮的文本。

Try changing your code to the following:尝试将您的代码更改为以下内容:

import React, { Component } from 'react'
import {Button} from 'primereact/button'

class Calculator extends Component {

  constructor(props) {
    super(props);
    this.state = {

    }
  }

  render() {
    const values1 = ["7", "8", "9", "/"]
    const values2 = ["4", "5", "6", "x"]
    const values3 = ["1", "2", "3", "-"]
    const values4 = ["0", ".", "+", "="]
    const valuesList1 = values1.map(value => <td><Button label={value} /></td>)
    const valuesList2 = values2.map(value => <td><Button label={value} /></td>)
    const valuesList3 = values3.map(value => <td><Button label={value} /></td>)
    const valuesList4 = values4.map(value => <td><Button label={value} /></td>)

    return (
      <div className="Middle">
        <section className="InputContainer">
          <table className="Keys">
            <tbody>
              <tr>
                {valuesList1}
              </tr>
              <tr>
                {valuesList2}
              </tr>
              <tr>
                {valuesList3}
              </tr>
              <tr>
                {valuesList4}
              </tr>
            </tbody>
          </table>
        </section>
      </div>
    );
  }

}

export default Calculator;

Hope this helps希望这可以帮助

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

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