- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我遵循了教程并得出了这个 https://jsbin.com/foxoxejilu/1/edit?js,output使用 react.js。
我对 Prop 和状态感到困惑。在Note组件
的save
方法中,这一行做了什么
this.props.onChange(this.refs.newText.value, this.props.id)
而且我在代码的其他地方没有看到 onChange 和 onRemove 函数,这是 React 的预构建函数吗? React 如何知道 DOM 已更改或删除?
最佳答案
我们可以这样总结正在发生的事情:
一位家长 (Board
) 将她的 child (Note
) 打包成一个袋子 (props
) 并放了一部手机 (onChange
) 并说:如果你想告诉我一些事情,请给我发短信.然后过了一段时间, child 想“让我们给我的 parent 发短信”,所以 child 从他的属性(property)中取出电话并发短信给 parent :this.props.onChange("Thank you for sandwiches")
. parent 随后收到消息并将其保存到她的笔记本中 (state
):this.setState({notes})
这是 child 接收属性(props)的地方:
<Note key={note.id}
id={note.id}
onChange={this.update}
onRemove={this.remove}>
{note.note}
</Note>
当上面找到通往 render()
的路时Board
的方法组件(通过 {this.state.notes.map(this.eachNote)}
,就像 Board
说,
Note
组件并赋予它一些属性 (props)”。 onChange
,当它稍后被 Note
调用时,让我们执行我自己的名为 update
的方法”onChange
,例如 <Note ... fromParentWithLove={this.update}> ... </Note>
,但我们将其命名为 onChange
,以便更容易遵循我们的意图。”<然后,我们可以移动到子组件 - Note
. Note
对自己说:
save
方法:” <button onClick={this.save}>Save</button>
save
方法中,让我们调用我的 parent onChange
通过 props 给我的 Board
函数”: this.props.onChange(this.refs.newText.value, this.props.id)
"作为引用,下面是从 JSBin 复制的代码:
var Note = React.createClass({
getInitialState(){
return {editing: false}
},
edit() {
this.setState({editing: true})
},
save() {
this.props.onChange(this.refs.newText.value, this.props.id)
this.setState({editing: false})
},
remove() {
this.props.onRemove(this.props.id)
},
renderForm() {
return(
<div className="note">
<textarea ref="newText"></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() {
return (this.state.editing) ? this.renderForm() : this.renderDisplay()
}
})
var Board = React.createClass({
propTypes: {
count: function(props, propName) {
if(typeof props[propName] !== 'number'){
return new Error('the count must be a number')
}
}
},
getInitialState() {
return {
notes: [
{id:1, note:'Call boook'},
{id:2, note:'Buy Something'},
{id:3, note:'Wash Clothes'},
{id:4, note:'Go Jogging'}
]
}
},
update(newText, id){
var notes = this.state.notes.map(
note => (note.id !== id) ?
note:
{
...note,
note:newText
}
)
this.setState({notes})
},
remove(id){
var notes = this.state.notes.filter(note => note.id !== id)
this.setState({notes})
},
eachNote(note) {
return (<Note key={note.id}
id={note.id}
onChange={this.update}
onRemove={this.remove}>
{note.note}
</Note>)
},
render() {
return (<div className='board'>
{this.state.notes.map(this.eachNote)}
</div>)
}
})
ReactDOM.render(<Board count={10}> </Board>,
document.getElementById('react-container'))
关于javascript - react.js 中 props 和 state 的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39804273/
我正在编写一个简单的有限状态机,并意识到在某些情况下,事件可以将状态变为多个可能的结果。基本上,从状态 A,如果事件 E 发生,状态可能是 C 或 D。 我目前正在使用此处编写的 Javascript
我在 React 中构建了一个应用程序,我在其中一个样板项目中找到了一行。 (state = {}) => state 谁能给我解释一下上面这行是什么意思?它是 javascript ES6 标准。
如何将多个状态变量组合成另一个? 我想通过一些用户交互来更改高度或宽度的值,并相应地更新 View 中的所有内容。所以高度或宽度会改变,面积也会改变。 我想它看起来像这样 @State var wid
我的容器正在通过 redux store 获取状态。 我通过这样的 Prop 将这个状态传递给模态框:示例: render(){ let {team,id} =this.props.data;
您好,我正在尝试使用 map 根据我所在状态的数组渲染选项,但在返回中使用它时我得到未定义 这是数组 this.state = { countries: ["Australia","Brazil"
我想将 this.state.currentPlayer 分配给 this.state.whosPlaying。它抛出错误TypeError:无法读取新板上未定义的属性“currentPlayer”。
我正在实现某种动态工作流程,当达到某个点时,我必须重新加载状态以呈现 HTML 并重新实例化 Controller 才能继续。 我发现我第二次调用 $state.reload() 不起作用。这是期望的
我正在开发一个 flutter 应用程序,并发现状态管理出现意外行为。我创建了一个示例应用来重现该行为,您可以在下面找到代码和日志输出。 该应用程序包含一个简单的 ListView,其中包含 10 个
有人可以举一个简单的例子,其中 state monad 比直接传递 state 更好吗? bar1 (Foo x) = Foo (x + 1) 对比 bar2 :: State Foo Foo bar
我想写类似 $state.go("/spheres/{{$stateParams.sphereId}}/mono/view"); 的内容使用外部 url 而不是状态,但这不起作用:( 现在我明白为什么
我正在使用“angular-ui-tree”:“^2.22.5” 点击执行某事菜单项时出错.. TypeError: this.$state is undefined 如何将对 $state 的引用传
我在elasticsearch中有文本字段,我想在kibana上可视化词云... 第一步,我们需要标记它们,我使用了“标准标记器” ... 使用这种形式的词云可视化结果如下图所示: 但是我需要的是专有
我正在尝试以编程方式在状态之间移动(使用 ui.router),而用户无需单击任何内容。文档位于 http://angular-ui.github.io/ui-router/site/#/api/ui
我想编写像“(event, state) -> state”这样的折叠函数。如果Java中没有任何模式匹配且不可变,我该如何编写它? 最佳答案 我认为您正在寻找 Java 中的函数式编程。 此版本中引
这个问题已经有答案了: What does an exclamation mark before a variable mean in JavaScript (4 个回答) 已关闭 8 年前。 您好,
https://plnkr.co/edit/bOZW1a9u62W1QA6cYjYj?p=preview 预期 登录后,所有 $states 都会初始化,然后单击 Ticker 按钮后,唯一应重新初始
试图决定(针对我的应用程序)在 onPause() 中保存什么以及要保存在 onSaveInstanceState() 中的内容,我梳理了整个 SO 以获得提示和明确的指导方针。 如果我没理解错的话,
在 Javascript 中,当我单击滚动条(页面中出现的任何滚动条)并将鼠标悬停在图像上时,图像再次开始拖动。 图像只能在鼠标按钮按下状态下拖动。 所以我试图通过了解鼠标按钮状态(mousedown
我见过 Maybe和 Either在代码中使用仿函数(和应用)是有道理的,但我很难想出一个 State 的例子。仿函数和应用。也许它们不是很有用,只是因为 State 才存在。 monad 需要一个仿
我非常努力地想围绕 State Monad,但我不明白以下内容: 鉴于 return 的实现和 (>>=) ,当你说 State $ \s ->.... ,在哪里s来自?我的意思是,当你开始表演时 >
我是一名优秀的程序员,十分优秀!