我有两个文件,第一个是todoHelper.js
它有 export const addTodo = (list, item) => [...list, item]
后来我想addTodo在另一个文件中使用,我只是这样做import {addTodo} from './todoHelpers'
但我也看到人们做出口违约而不仅仅是出口.有什么区别?
许多人提倡不变性,因为他们完全使用redux和反应,但我仍然看到人们使用push而不是concat.
以此代码为例:
submitComment() {
console.log('submitComment: '+JSON.stringify(this.state.comment))
APIManager.post('/api/comment', this.state.comment, (err, response) => {
if (err){
alert(err)
return
}
console.log(JSON.stringify(response))
let updateList = Object.assign([], this.state.list)
updatedList.push(response.result)
this.setState({
list: updatedList
})
})
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它有关系吗?推送上面的问题是什么?
我正在关注反应教程,但我迷路了.我不明白第9行.
所以我试着做一点温度
const updateTodo = (list, updated) => {
const index = list.findIndex(item => item.id === updated.id)
return [
...list.slice(0,index),
updated,
...list.slice(index+1)
]
}
Run Code Online (Sandbox Code Playgroud)
https://jsbin.com/sifihocija/2/edit?js,console但未能产生作者所做的结果,出了什么问题?
尝试避免Component的扩展,因为组件只是呈现数据,它可以是独立的.
const CountDown = () => {
render(){
return(
<p>Countdown.jsx</p>
)
}
}
module.exports = Countdown
Run Code Online (Sandbox Code Playgroud)
但是怎么了?我得到了意想不到的令牌render(){ ... }