javascript - 如何在 Click React 上创建组件

标签 javascript reactjs meteor

我是 React 的新手,我正在尝试创建一个 Web 应用程序,当我单击一个按钮时它会创建 Rnd 组件。当我单击该按钮时,我只能让它创建一个 Rnd 组件,即使它仍然记录了所有的点击。任何帮助都会很棒。

import { Meteor } from 'meteor/meteor';
import React from 'react';
import ReactDOM from 'react-dom';
import { render } from 'react-dom';
import Rnd from 'react-rnd';

export default class App extends React.Component {

  renderWidget() {

    console.log('I was clicked');

    const widget = React.createElement(Rnd, {default: {x: 0, y: 0, width: 320, height: 200}, className: 'box'}, React.createElement('p', {}, this.state.text));

    ReactDOM.render(
      widget,
      document.getElementById('widget')
    );
  }

  render () {
    return (
      <div>
        <section className='hero is-dark'>
          <div className='hero-body'>
            <div className='container'>
              <h1 className='title'>
                Dashboard
              </h1>
              <h2 className='subtitle'>
                At A Glance Data
              </h2>
            </div>
          </div>
        </section>
        <section className='section'>
          <div className='container has-text-right'>
            <h2 className='subtitle'>
              Create New Widget
            </h2>
            <button className='button is-dark is-outlined' onClick={this.renderWidget.bind(this)}>
              <span className='icon'>
                <i className='fas fa-plus'></i>
              </span>
            </button>
          </div>
          <div id='widget'>
          </div>
        </section>
      </div>
    );
  }
}

最佳答案

这是您可以做您想做的事情的一种方法:

import React from "react";
import { render } from "react-dom";
import Rnd from "react-rnd";

const Widget = () => <p>Hello World</p>;

export default class App extends React.Component {
  constructor() {
    super();

    this.state = {
      components: [],
      text: "Hello to you"
    };
  }

  renderWidget() {
    console.log("I was clicked");

    const newComponents = [...this.state.components, Widget];

    this.setState({
      components: newComponents
    });
  }

  render() {
    const { components } = this.state;
    return (
      <div>
        <section className="hero is-dark">
          <div className="hero-body">
            <div className="container">
              <h1 className="title">Dashboard</h1>
              <h2 className="subtitle">At A Glance Data</h2>
            </div>
          </div>
        </section>
        <section className="section">
          <div className="container has-text-right">
            <h2 className="subtitle">Create New Widget</h2>
            <button
              className="button is-dark is-outlined"
              onClick={this.renderWidget.bind(this)}
            >
              <span className="icon">
                <i className="fas fa-plus" />
                Click me
              </span>
            </button>
          </div>
          <div>
            {components.length !== 0 &&
              components.map((Widget, i) => <Widget key={i} />)}
          </div>
        </section>
      </div>
    );
  }
}

render(<App />, document.getElementById("widget"));

Edit xlknzw4yr4

关于javascript - 如何在 Click React 上创建组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49395033/

相关文章:

reactjs - Typescript 中的 React 事件类型

javascript - Meteor 不加载外部 CSS 和 JS 文件

JavaScript:不可枚举的属性 - 何时何地?

javascript - 动态表 Javascript - Angular 2

javascript - 如何测试两个 NodeLists 的相等性?

angular - 像 AngularJS 中的作用域注入(inject),用于更现代的库

javascript - 将文件直接从浏览器发送到 S3 但更改文件名

reactjs - 如果数据存在于 redux 存储中,则阻止 API 调用

javascript - 用户空格键和模板助手在 Meteor 中随机加载页面背景

javascript - 从 Meteor 移动应用程序向 S3 进行身份验证