我是react的初学者,我已经编写了以下代码:
class Note extends React.Component {
constructor(props) {
super(props);
this.state = {editing: false};
this.edit = this.edit.bind(this);
this.save = this.save.bind(this);
}
edit() {
// alert('edit');
this.setState({editing: !this.state.editing});
}
save() {
this.props.onChange(this.refs.newVal.value, this.props.id);
this.setState({editing: !this.state.editing});
// console.log('save is over');
}
renderForm() {
return (
<div className="note">
<textarea ref="newVal"></textarea>
<button onClick={this.save}>SAVE</button>
</div>
);
}
renderDisplay() {
return (
<div className="note">
<p>{this.props.children}</p>
<span>
<button onClick={this.edit}>EDIT</button>
<button onClick={this.remove}>X</button>
</span>
</div>
);
}
render() {
console.log(this.state.editing);
return (this.state.editing) ? this.renderForm()
: this.renderDisplay()
}
}
class Board extends React.Component {
constructor(props){
super(props);
this.state = {
notes: []
};
this.update = this.update.bind(this);
this.eachNote = this.eachNote.bind(this);
this.add = this.add.bind(this);
}
nextId() {
this.uniqeId = this.uniqeId || 0;
return this.uniqeId++;
}
add(text) {
let notes = [
...this.state.notes,
{
id: this.nextId(),
note: text
}
];
this.setState({notes});
}
update(newText, id) {
let notes = this.state.notes.map(
note => (note.id !== id) ?
note :
{
id: id,
note: newText
}
);
this.setState({notes})
}
eachNote(note) {
return (<Note key={note.id}
id={note.id}
onChange={this.update}>
{note.note}
</Note>)
}
render() {
return (<div className='board'>
{this.state.notes.map(this.eachNote)}
<button onClick={() => this.add()}>+</button>
</div>)
}
}
ReactDOM.render(<Board />,
document.getElementById('root'));
在render()中,onClick event有一个函数,即,如果以这种方式使用:{this.add}将创建以下错误:
未捕获错误:对象作为React子对象无效(已找到:带键的对象{调度配置,_targetInst,native事件,类型,目标,当前目标,事件阶段,气泡,可取消,时间戳,默认预防,isTrusted,视图,详细信息,...})
为什么?在eachNote()方法中使用此命令时:
onChange={this.update}
没有错误。
有人能告诉我原因吗?谢谢。
class Note extends React.Component {
constructor(props) {
super(props);
this.state = {editing: false};
this.edit = this.edit.bind(this);
}
edit() {
// alert('edit');
this.setState({editing: !this.state.editing});
}
render() {
return (
<div>Render something</div>
)
}
}
class Board extends React.Component {
constructor(props){
super(props);
this.state = {
notes: []
};
this.update = this.update.bind(this);
this.eachNote = this.eachNote.bind(this);
this.add = this.add.bind(this);
}
nextId() {
this.uniqeId = this.uniqeId || 0;
return this.uniqeId++;
}
add(text) {
let notes = [
...this.state.notes,
{
id: this.nextId(),
note: text
}
];
this.setState({notes});
}
update(newText, id) {
let notes = this.state.notes.map(
note => (note.id !== id) ?
note :
{
id: id,
note: newText
}
);
this.setState({notes})
}
eachNote(note) {
return (<Note key={note.id}
id={note.id}
onChange={this.update}>
{note.note}
</Note>)
}
render() {
return (<div className='board'>
{this.state.notes.map(this.eachNote)}
<button onClick={() => this.add()}>+</button>
</div>)
}
}
ReactDOM.render(<Board />,
document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
<div id="root"></div>
onClick={this.add}
将单击事件传递给this.add
。
所以你需要做的是:
onClick={e=
如果你想
onClick={this.add}
你必须确保你的add方法是:add(event){…}
。
问题是,在add函数中,您正在获取一个参数文本,并将其设置为状态,因此当您调用onClick={()=
但是,如果您直接调用它,如
on点击={this.add}
,则add
函数将事件
对象作为参数接收,因此它将state note
设置为事件对象你用它来渲染代码
问题内容: 我正在编写一个游戏引擎,其中使用for循环迭代保存在a 中的一组对象。显然,效率非常重要,因此我想知道循环的效率。 当返回的秒。我想知道的是,每次循环在新扩展上进行迭代时,是否都调用该方法。如果是这样,这样做会更有效: ?提前致谢。 问题答案: 按照规范,这个成语 扩展成 因此,您询问的呼叫在循环初始化时仅发生一次。这是迭代器对象,其方法被重复调用。 但是,如果您真的对应用程序的性能感
用户在移动设备上面临的网络条件很容易被忽略。使用DevTools来模拟不同的网络条件。修复加载时间问题,您的用户将感谢您。 TL;DR 在不影响其他标签的流量的情况下,使用Chrome DevTools网络模拟器来评估网站的性能。 使用自定义配置文件,配置特定的受众群体网络条件。 模拟网络连接 Network Conditioning(网络调节)允许您在各种网络环境中测试您的网站,包括Edge,3
本文向大家介绍python在不同条件下的输入与输出,包括了python在不同条件下的输入与输出的使用技巧和注意事项,需要的朋友参考一下 1. 用户输入内容与打印 输入:input() 输出:print() 例1,输入字符串,并原样输出 例2,输入字符串,并判断是否是回文,打印结果 2. 文件创建、读写 打开文件:f = open('文件名', '打开模式') 读取:f.read()、f.readl
目前我正在研究文本字段和默认/取消按钮的问题。在使用TestFX测试假定的修复时,我遇到了事件调度方面的差异(?)这使得测试在应用程序运行时失败。 下面是一个非常简化的版本: 只是一个简单的用户界面,由一个框中的文本字段组成,用于应用程序/测试 修复程序的关键部分是创建actionEvent,将textField同时作为源和目标(以防止在调度期间将事件复制到新实例中): 运行应用程序时,这似乎足以
我想处理的情况下401,403,500等只是状态代码应该返回而不是视图。
我带着我的下一个问题来找你xd这是我的JS: 为什么不使用“addClass(”d-block“)函数呢?但是我的”else“很好。 这是我的HTML