21CTO社区导读:React证书问题已解决,越来越多的公司使用React,前端交互式编程技术已经越来越火热了。本文是React作者在React设计之初,对整个框架的思考,希望对各位前端开发者有益。
functionNameBox(name){
return{fontWeight:'bold',labelContent:name };
}
'Sebastian Markbåge'->
{fontWeight:'bold',labelContent:'Sebastian Markbåge'};
functionFancyUserBox(user){
return{
borderStyle:'1px solid blue',
childContent:[
'Name: ',
NameBox(user.firstName +' '+user.lastName)
]
};
}
{firstName:'Sebastian',lastName:'Markbåge'}->
{
borderStyle:'1px solid blue',
childContent:[
'Name: ',
{fontWeight:'bold',labelContent:'Sebastian Markbåge'}
]
};
上述代码FancyUserBox()和NameBox() 嵌套
functionFancyBox(children){
return{
borderStyle:'1px solid blue',
children:children
};
}
functionUserBox(user){
returnFancyBox([
'Name: ',
NameBox(user.firstName +' '+user.lastName)
]);
}
其实这里说的组合就是多个组件(数组)可以作为一个整体渲染;这里说的抽象,其实就是组件。
functionFancyNameBox(user,likes,onClick){
returnFancyBox([
'Name: ',NameBox(user.firstName +' '+user.lastName),
'Likes: ',LikeBox(likes),
LikeButton(onClick)
]);
}
// Implementation Details
varlikes =0;
functionaddOneMoreLike(){
likes++;
rerender();
}
// Init
FancyNameBox(
{firstName:'Sebastian',lastName:'Markbåge'},
likes,
addOneMoreLike
);
这个例子在更新状态的时候有副作用。我真实的构思是在更新过程中函数返回下一个状态。这里是用最简单的方式给大家阐述原理,以后我们会更新这个例子。
functionmemoize(fn){
varcachedArg;
varcachedResult;
returnfunction(arg){
if(cachedArg ===arg){
returncachedResult;
}
cachedArg =arg;
cachedResult =fn(arg);
returncachedResult;
};
}
varMemoizedNameBox =memoize(NameBox);
functionNameAndAgeBox(user,currentTime){
returnFancyBox([
'Name: ',
MemoizedNameBox(user.firstName +' '+user.lastName),
'Age in milliseconds: ',
currentTime -user.dateOfBirth
]);
}
functionUserList(users,likesPerUser,updateUserLikes){
returnusers.map(user =>FancyNameBox(
user,
likesPerUser.get(user.id),
()=>updateUserLikes(user.id,likesPerUser.get(user.id)+1)
));
}
varlikesPerUser =newMap();
functionupdateUserLikes(id,likeCount){
likesPerUser.set(id,likeCount);
rerender();
}
UserList(data.users,likesPerUser,updateUserLikes);
我们将多个不同的参数传给了FancyNameBox,这个功能和记忆模块冲突了,因为我们每次只能记住一个值.接下来我们会讨论这点 .
functionFancyUserList(users){
returnFancyBox(
UserList.bind(null,users)
);
}
constbox =FancyUserList(data.users);
constresolvedChildren =box.children(likesPerUser,updateUserLikes);
constresolvedBox ={
...box,
children:resolvedChildren
};
functionFancyBoxWithState(
children,
stateMap,
updateState
){
returnFancyBox(
children.map(child =>child.continuation(
stateMap.get(child.key),
updateState
))
);
}
functionUserList(users){
returnusers.map(user =>{
continuation:FancyNameBox.bind(null,user),
key:user.id
});
}
functionFancyUserList(users){
returnFancyBoxWithState.bind(null,
UserList(users)
);
}
constcontinuation =FancyUserList(data.users);
continuation(likesPerUser,updateUserLikes);
functionmemoize(fn){
returnfunction(arg,memoizationCache){
if(memoizationCache.arg ===arg){
returnmemoizationCache.result;
}
constresult =fn(arg);
memoizationCache.arg =arg;
memoizationCache.result =result;
returnresult;
};
}
functionFancyBoxWithState(
children,
stateMap,
updateState,
memoizationCache
){
returnFancyBox(
children.map(child =>child.continuation(
stateMap.get(child.key),
updateState,
memoizationCache.get(child.key)
))
);
}
constMemoizedFancyNameBox =memoize(FancyNameBox);
functionThemeBorderColorRequest(){}
functionFancyBox(children){
constcolor =raise newThemeBorderColorRequest();
return{
borderWidth:'1px',
borderColor:color,
children:children
};
}
functionBlueTheme(children){
returntry{
children();
}catcheffect ThemeBorderColorRequest ->[,continuation]{
continuation('blue');
}
}
functionApp(data){
returnBlueTheme(
FancyUserList.bind(null,data.users)
);
}
译者:ramroll
译文:https://zhuanlan.zhihu.com/p/30277192
原文:https://github.com/reactjs/rea ... ME.md
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。