【问题标题】:Rendering a Bootstrap Component Using JSX使用 JSX 渲染引导组件
【发布时间】:2020-06-16 08:56:33
【问题描述】:

为清楚起见进行编辑:我无法弄清楚如何在 react 应用程序中使用 JSX 动态创建 Boostrap 组件。最终目标是在单击第一个按钮时在“newBtnSpace”div 中获取新按钮。我尝试过使用 show.hide 方法,但这些方法需要硬编码。尝试基于数组创建按钮。代码:

./components/newBSBtnSpaceFunc.js

    import React, { Component } from 'react'
    import { Button } from 'reactstrap'

    export default function NewBSBtnFunc() {
    let BtnArray = ["red", "blue", "green"].map((btn) => {
        return React.createElement(
            Button,
            {variant: 'primary'},
            'New Button',
            {id: "newBtn"},
             btn
            )   
           }

./components/BSBtn.js

     import React, { Component } from 'react'
     import { Button } from 'reactstrap'
     import NewBSBtnFunc from "./NewBSBtnFunc"


     export default class BSBtn extends Component {

     render() {
       return (
          <div>
            <Button onClick={NewBSBtnFunc}>Click Me</Button>
            <div id="newBtnSpace"></div>
          </div>
       )
      }

}

App.js

     import React from 'react';
     import 'bootstrap/dist/css/bootstrap.min.css';
     import BSBtn from "./components/BSBtn"


     function App() {
      return (
       <div>
        <BSBtn></BSBtn>
      </div>
     );
    }

   export default App;

github链接:https://github.com/mollygilbert389/testingBootstrapBtn

【问题讨论】:

    标签: reactjs button components reactstrap


    【解决方案1】:

    您可以通过在原始按钮的onClick 中将状态项(在本例中为showNewButton)设置为true 来有条件地显示新按钮。

        render() {
            return (
                <div>
                    <Button onClick={() => this.setState({ showNewButton: true }))}>Click Me</Button>
                    <div id="newBtnSpace">{ this.state.showNewButton && <Button variant="primary" id="newBtn">New Button</Button> }</div>
                </div>
            )
        }
    

    PS 你已经成功解决了如何在 jsx 中创建 Bootstrap 按钮:

    <Button onClick={NewBSBtnFunc}>Click Me</Button>
    

    【讨论】:

    • 谢谢,但我不想用显示/隐藏方法来做到这一点。需要动态创建。使用显示/隐藏我需要在那里对按钮进行硬编码。试图让它根据一组单词创建按钮。
    • 您根本不需要在其中对按钮进行硬编码。例如:words.map(word =&gt; &lt;Button&gt;{word}&lt;/Button&gt;);
    【解决方案2】:

    onClick 不期望返回值,因此返回新按钮不会做任何事情。

    您组织事物的方式非常困难,因为您无法从函数返回任何内容,也无法从类外部修改状态。我建议将您的点击处理程序移动到组件中并使用 to 修改将显示第二个按钮的状态值。

    这是我的建议:

    import React, { Component } from 'react'
    import { Button } from 'reactstrap'
    
    export default class BSBtn extends Component {
      state = {show: false}
    
      handleClick = () => {
        this.setState({ show: !this.state.show })
      }
    
      render() {
        return (
          <div>
            <Button onClick={this.handleClick}>Click Me</Button>
            <div id="newBtnSpace">
              {this.state.show ? 
                <Button variant="primary" id="newBtn">New Button</Button>
              : null}
             </div>
          </div>
        )
      }
    }
    

    更新后的问题的解决方案:

    class BSBtn extends React.Component {
      state = {
        show: false,
        buttons: []
      }
    
      handleClick = () => {
        this.setState({ show: !this.state.show })
      }
      
      handleAdd = () => {
        this.setState({ buttons: [...this.state.buttons, (this.state.buttons.length + 1)] })
      }
    
      render() {
        return (
          <div>
            <h3>Option 1</h3>
            <button onClick={this.handleClick}>Click Me</button>
            <div id="newBtnSpace">
              {this.state.show ? [1,2,3].map((value) => (
                <div>
                  <button>Button {value}</button>
                </div>
             ))
             : null}
             </div>
             <hr/>
             <div style={{ marginTop: '30px' }}>
              <h3>Option 2</h3>
              <button onClick={this.handleAdd}>Click Me</button>
              {this.state.buttons.map((value) => (
                <div>
                  <button>Button {value}</button>
                </div>
             ))}
             </div>
          </div>
        )
      }
    }
    
    ReactDOM.render(<BSBtn />, document.getElementById('root'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    <div id='root' />

    【讨论】:

    • 谢谢,如上所述,我不想显示或隐藏我想动态创建专门的引导按钮。尝试根据数组中的名称创建一个按钮,这里需要使用 show 方法对其进行硬编码。
    • 我认为您需要进一步澄清您的问题。期望的输出是什么?当您单击第一个按钮时,您不想显示整个按钮阵列吗?您想为每次点击添加一个按钮吗?
    • 也只是一个提示:很少需要使用createElement。只需使用 JSX。
    猜你喜欢
    • 2020-10-12
    • 2017-10-16
    • 2019-05-17
    • 2016-06-22
    • 2020-10-12
    • 2021-01-11
    • 2017-10-11
    • 2018-03-07
    • 2017-10-12
    相关资源
    最近更新 更多