huzidaha / react-naive-book-examples Goto Github PK
View Code? Open in Web Editor NEWExamples for React Naive Book
License: MIT License
Examples for React Naive Book
License: MIT License
可以将多次 dispatch 在某个时间段合并成一个操作
`function createStore(s,stateChanger) {
const listeners = []
const queue = []
let state = s
const getState = () => state
let timerId :any = 0
const dispatch = (action) => {
console.log(action)
if(typeof action == 'function') {
const d = stateChanger(state,action())
for(const value of listeners) {
value(d)
state = d
}
return;
}
queue.push(() => {
return stateChanger(state,action)
})
clearImmediate(timerId)
timerId = setImmediate (() => {
let d = null
while (queue.length) {
d = queue.shift()()
state = d
}
for(const value of listeners) {
value(d)
}
},1)
}
const subscribe = (listen) => listeners.push(listen)
return { getState,dispatch,subscribe }
}`
CommnetInput>handleSubmit()中,建议加上一句判断,如果用户名为空,则除了提示输入用户名之外,应该保留内容,这样应该更符合实际。
`` handleSubmit () {
if (this.props.onSubmit) {
const user = this.state.user
const content = this.state.content
// const { user, content } = this.state
this.props.onSubmit({ user, content })
}
if (!this.state.user)
return//如果用户名为空,则到此返回
this.setState ({ content:"" })
}
不知道为什么我这边的CommentInput从localStorage获取出来的username无法更新当前的state,现在是直接在初始化的时候从localStore取得赋值给state.username
我刚刚学react,看见您写的小书对我帮助很大。感谢
function createStore(s,stateChanger) {
const listeners = []
const queue = []
let state = s
const getState = () => state
let timerId :any = 0
const dispatch = (action) => {
if(typeof action == 'function') {
const d = stateChanger(state,action())
for(const value of listeners) {
value(d)
state = d
}
return;
}
queue.push(() => {
return stateChanger(state,action)
})
clearImmediate(timerId)
timerId = setImmediate (() => {
let d = null
while (queue.length) {
d = queue.shift()()
state = d
}
for(const value of listeners) {
value(d)
}
},1)
}
const subscribe = (listen) => listeners.push(listen)
return { getState,dispatch,subscribe }
}
在comments.js中
case DELETE_COMMENT:
// 删除评论
return {
comments: [
...state.comments.slice(0, action.commentIndex),
...state.comments.slice(action.commentIndex + 1)
]
}
改为
case DELETE_COMMENT:
//删除评论,记住 index!
// console.log(state.comments,222222222)
let aa=state.comments.slice(0,action.commentIndex)
let bb=state.comments.slice(action.commentIndex+1)
return {
comments: [
...aa,
...bb
]
}
就不会报错了,不知道为什么,请赐教
这个教程已经两年多了,现在的React版本中,PropTypes独立出一个模块,旧代码中如果不改变引入方式将会报错。建议后来的读者可以参考一下:
ReactJS “TypeError: Cannot read property 'array' of undefined”
_getProcessedContent(content){
return content
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/([\S\s]+?)
/g, '$1
')
}
加了断点,代码也运行到return了,但是就是没有加上code,这是怎么回事呢
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.