【发布时间】:2018-08-29 21:44:36
【问题描述】:
我对 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>
);
}
}
【问题讨论】:
-
以下是正确的做法:codesandbox.io/s/91x4rkkn5r 你不应该在你的类中渲染 DOM,你需要阅读
state之类的东西。 -
感谢所有这些真的很有帮助。
-
@Atomator,你觉得我的回答有用吗?请点赞。谢谢。
-
@JohnKennedy,我确实投了赞成票,但我没有足够的声誉让它公开出现。
-
@Atomator,您可能希望接受它作为正确答案。这将被公开反映。谢谢。
标签: javascript reactjs meteor