是很久以前写的一个东西了,当时在学 Hooks 的时候想要找一个实现 todomvc 的例子来作为 demo 学习,网上找到的一个例子是: https://github.com/sokra/todo-mvc-react-hooks-experiments, 但它只用了 useState 而没有用 redux,于是就把 react 最经典的一个 todomvc 用 useState/useSelector/useDispatch
和纯 function class
改写了一遍。顺便除去了所有的 container component 让逻辑更集中一些。
const Link = ({ filter,children }) =>{
const active = useSelector(state=>state.visibilityFilter===filter)
const dispatch=useDispatch();
return (
// eslint-disable-next-line jsx-a11y/anchor-is-valid
<a
className={classnames({ selected: active })}
style={{ cursor: 'pointer' }}
onClick={() =>{dispatch(setVisibilityFilter(filter))}}
>
{children}
</a>
)}
}
因为没有与网络的交互就暂时没有用 useEffect 去实现各种生命周期函数。整个没什么太大难度吧,如果以后有同学想看 redux hooks 的例子可以做个参考。
repo : https://github.com/Allianzcortex/code_collection/tree/dev/react-todomvc-only-hooks-without-mapstate
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.