vuejs和reactjs中,关于状态管理的替代【vuex/redux】
我们在写前端项目时候,有时候不得不在组件中更新兄弟父亲姐妹的各种状态,
VUEJS的解决办法是vuex,reactjs是redux,
都是定义全局state, 通过action=>dispatch来更新状态,并且通过持久化插件写入session 或者local stroge中,
随着项目的复杂,vuex|redux越来越复杂,已经违背的设计者的初衷,
尤其是reactjs: 尽量不用redux就坚决别用。在react hooks中就体现了这一点,
那么怎么脱离这个复杂的state=>action=>dispatch逻辑呢?
这里推荐一个插件:lowdb.
项目地址:https://github.com/typicode/lowdb
在项目中创建utils/db.js
import low from 'lowdb'
import LocalStorage from 'lowdb/adapters/LocalStorage'
const adapter = new LocalStorage('your appaction storage name')
const db = low(adapter)
const store = { token: '', username: '', user: {} }
db.defaults(store).write()
export default db
vuejs中,在App.vue项目中初始化db:
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
import db from './libs/db'
export default {
name: 'App',
data () {
return {
db: db
}
}
}
</script>
在reactjs中,创建一个db的provider组件:
import react, {useEffect} from 'React'
import db from './libs/db'
const ProviderForDB = (props)=>{
useEffect(()=>{
const store = { token: '', username: '', user: {} }
db.defaults(store).write()
}, [])
return <div>{props.children}</div>
}
export defualt ProviderForDB;
使用方法:
db.get('posts')
.push({ id: 1, title: 'lowdb is awesome'})
.write()
// Set a user using Lodash shorthand syntax
db.set('user.name', 'typicode')
.write()
// Increment count
db.update('count', n => n + 1)
.write()
具体的案例:
登录:
import db from './libs/db'
this.login({ username: this.username, password: this.password }).then(res => {
db.set('token', res.data.token).set('username', res.data.username).set('user', res.data.user).write()
})
注销:
import db from './libs/db'
this.login({ username: this.username, password: this.password }).then(res => {
db.set('token', '').set('username', '').set('user', {}).write()
})
通过db的方法来实现状态的更新,并且持久化。
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
driving while taking prescription drugs https://edmeds.buszcentrum.com/
vidalista without a doctors approval http://vidallista.com/
I'm just writing to make you be aware of what a perfect discovery my girl obtained going through your webblog. She even learned numerous things, not to mention what it's like to have an incredible helping nature to make most people clearly learn about certain complicated matters. You really surpassed people's expected results. I appreciate you for churning out the practical, safe, informative and in addition unique tips on your topic to Ethel.
walgreens price for cialis 20mg http://ciaalis2u.com/
online medical diagnosis and treatment https://medpills.bee-rich.com/
male parasites supplements http://ivermmectin.com/
I wish to show some thanks to the writer just for bailing me out of such a situation. Because of surfing throughout the search engines and getting principles which are not pleasant, I believed my entire life was done. Being alive without the solutions to the problems you've resolved as a result of your main report is a critical case, as well as ones which may have in a wrong way damaged my entire career if I had not noticed your web site. The understanding and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. It's possible to now look forward to my future. Thank you so much for your specialized and results-oriented guide. I will not be reluctant to recommend your web blog to anyone who would like care on this issue.
I must express my respect for your kindness for men and women who must have help with the theme. Your very own commitment to getting the message throughout had been pretty good and has all the time permitted ladies just like me to achieve their dreams. Your own informative guide signifies a whole lot to me and even further to my colleagues. Thank you; from everyone of us.
purchasing cialis in the usa https://tadalafil.cleckleyfloors.com/
Thanks so much for giving everyone such a wonderful chance to read articles and blog posts from this site. It is often so useful plus packed with a lot of fun for me personally and my office colleagues to visit your site the equivalent of 3 times per week to read the newest guidance you will have. And indeed, I'm just certainly amazed considering the splendid solutions you give. Certain two facts in this post are in fact the very best we have all ever had.
I must convey my gratitude for your kindness in support of individuals who really need help with this niche. Your special dedication to getting the solution all around became surprisingly beneficial and has in every case allowed girls like me to arrive at their pursuits. Your new interesting publication denotes this much to me and far more to my mates. With thanks; from each one of us.