Redux异步解决方案之Redux

前段时间,我们写了一篇Redux源码分析的文章,也分析了跟React连接的库React-Redux的源码实现。但是在Redux的生态中还有一个很重要的部分没有涉及到,那就是Redux的异步解决方案。本文会讲解Redux官方实现的异步解决方案----Redux-Thunk,我们还是会从基本的用法入手,再到原理解析,然后自己手写一个Redux-Thunk来替换它,也就是源码解析。

Redux-Thunk和前面写过的ReduxReact-Redux其实都是Redux官方团队的作品,他们的侧重点各有不同:

Redux:是核心库,功能简单,只是一个单纯的状态机,但是蕴含的思想不简单,是传说中的“百行代码,千行文档”。

React-Redux:是跟React的连接库,当Redux状态更新的时候通知React更新组件。

Redux-Thunk:提供Redux的异步解决方案,弥补Redux功能的不足。

本文手写代码已经上传GitHub,大家可以拿下来玩玩:https://github.com/dennis-jiang/Front-End-Knowledges/blob/master/Examples/React/redux-thunk/src/myThunk.js

基本用法

还是以我们之前的那个计数器作为例子,为了让计数器+1,我们会发出一个action,像这样:

1function increment() { 2 return { 3 type: 'INCREMENT' 4 } 5}; 6 7store.dispatch(increment());

原始的Redux里面,action creator必须返回plain object,而且必须是同步的。但是我们的应用里面经常会有定时器,网络请求等等异步操作,使用Redux-Thunk就可以发出异步的action

1function increment() { 2 return { 3 type: 'INCREMENT' 4 } 5}; 6 7// 异步action creator 8function incrementAsync() { 9 return (dispatch) => { 10 setTimeout(() => { 11 dispatch(increment()); 12 }, 1000); 13 } 14} 15 16// 使用了Redux-Thunk后dispatch不仅仅可以发出plain object,还可以发出这个异步的函数 17store.dispatch(incrementAsync());

下面再来看个更实际点的例子,也是官方文档中的例子:

1import { createStore, applyMiddleware } from 'redux'; 2import thunk from 'redux-thunk'; 3import rootReducer from './reducers'; 4 5// createStore的时候传入thunk中间件 6const store = createStore(rootReducer, applyMiddleware(thunk)); 7 8// 发起网络请求的方法 9function fetchSecretSauce() { 10 return fetch('https://www.baidu.com/s?wd=Secret%20Sauce'); 11} 12 13// 下面两个是普通的action 14function makeASandwich(forPerson, secretSauce) { 15 return { 16 type: 'MAKE_SANDWICH', 17 forPerson, 18 secretSauce, 19 }; 20} 21 22function apologize(fromPerson, toPerson, error) { 23 return { 24 type: 'APOLOGIZE', 25 fromPerson, 26 toPerson, 27 error, 28 }; 29} 30 31// 这是一个异步action,先请求网络,成功就makeASandwich,失败就apologize 32function makeASandwichWithSecretSauce(forPerson) { 33 return function (dispatch) { 34 return fetchSecretSauce().then( 35 (sauce) => dispatch(makeASandwich(forPerson, sauce)), 36 (error) => dispatch(apologize('The Sandwich Shop', forPerson, error)), 37 ); 38 }; 39} 40 41// 最终dispatch的是异步action makeASandwichWithSecretSauce 42store.dispatch(makeASandwichWithSecretSauce('Me')); 43

为什么要用Redux-Thunk

在继续深入源码前,我们先来思考一个问题,为什么我们要用Redux-Thunk,不用它行不行?再仔细看看Redux-Thunk的作用:

1// 异步action creator 2function incrementAsync() { 3 return (dispatch) => { 4 setTimeout(() => { 5 dispatch(increment()); 6 }, 1000); 7 } 8} 9 10store.dispatch(incrementAsync());

他仅仅是让dispath多支持了一种类型,就是函数类型,在使用Redux-Thunk前我们dispatchaction必须是一个纯对象(plain object),使用了Redux-Thunk后,dispatch可以支持函数,这个函数会传入dispatch本身作为参数。但是其实我们不使用Redux-Thunk也可以达到同样的效果,比如上面代码我完全可以不要外层的incrementAsync,直接这样写:

1setTimeout(() => { 2 store.dispatch(increment()); 3}, 1000);

这样写同样可以在1秒后发出增加的action,而且代码还更简单,那我们为什么还要用Redux-Thunk呢,他存在的意义是什么呢?stackoverflow对这个问题有一个很好的回答,而且是官方推荐的解释。我再写一遍也不会比他写得更好,所以我就直接翻译了:

----翻译从这里开始----

**不要觉得一个库就应该规定了所有事情!**如果你想用JS处理一个延时任务,直接用setTimeout就好了,即使你使用了Redux也没啥区别。Redux确实提供了另一种处理异步任务的机制,但是你应该用它来解决你很多重复代码的问题。如果你没有太多重复代码,使用语言原生方案其实是最简单的方案。

直接写异步代码

到目前为止这是最简单的方案,Redux也不需要特殊的配置:

1store.dispatch({ type: 'SHOW_NOTIFICATION', text: 'You logged in.' }) 2setTimeout(() => { 3 store.dispatch({ type: 'HIDE_NOTIFICATION' }) 4}, 5000)

(译注:这段代码的功能是显示一个通知,5秒后自动消失,也就是我们经常使用的toast效果,原作者一直以这个为例。)

相似的,如果你是在一个连接了Redux组件中使用:

1this.props.dispatch({ type: 'SHOW_NOTIFICATION', text: 'You logged in.' }) 2setTimeout(() => { 3 this.props.dispatch({ type: 'HIDE_NOTIFICATION' }) 4}, 5000)

唯一的区别就是连接组件一般不需要直接使用store,而是将dispatch或者action creator作为props注入,这两种方式对我们都没区别。

如果你不想写重复的action名字,你可以将这两个action抽取成action creator而不是直接dispatch一个对象:

1// actions.js 2export function showNotification(text) { 3 return { type: 'SHOW_NOTIFICATION', text } 4} 5export function hideNotification() { 6 return { type: 'HIDE_NOTIFICATION' } 7} 8 9// component.js 10import { showNotification, hideNotification } from '../actions' 11 12this.props.dispatch(showNotification('You just logged in.')) 13setTimeout(() => { 14 this.props.dispatch(hideNotification()) 15}, 5000)

或者你已经通过connect()注入了这两个action creator

1this.props.showNotification('You just logged in.') 2setTimeout(() => { 3 this.props.hideNotification() 4}, 5000)

到目前为止,我们没有使用任何中间件或者其他高级技巧,但是我们同样实现了异步任务的处理。

提取异步的Action Creator

使用上面的方式在简单场景下可以工作的很好,但是你可能已经发现了几个问题:

  1. 每次你想显示toast的时候,你都得把这一大段代码抄过来抄过去。
  2. 现在的toast没有id,这可能会导致一种竞争的情况:如果你连续快速的显示两次toast,当第一次的结束时,他会dispatchHIDE_NOTIFICATION,这会错误的导致第二个也被关掉。

为了解决这两个问题,你可能需要将toast的逻辑抽取出来作为一个方法,大概长这样:

1// actions.js 2function showNotification(id, text) { 3 return { type: 'SHOW_NOTIFICATION', id, text } 4} 5function hideNotification(id) { 6 return { type: 'HIDE_NOTIFICATION', id } 7} 8 9let nextNotificationId = 0 10export function showNotificationWithTimeout(dispatch, text) { 11 // 给通知分配一个ID可以让reducer忽略非当前通知的HIDE_NOTIFICATION 12 // 而且我们把计时器的ID记录下来以便于后面用clearTimeout()清除计时器 13 const id = nextNotificationId++ 14 dispatch(showNotification(id, text)) 15 16 setTimeout(() => { 17 dispatch(hideNotification(id)) 18 }, 5000) 19}

现在你的组件可以直接使用showNotificationWithTimeout,再也不用抄来抄去了,也不用担心竞争问题了:

1// component.js 2showNotificationWithTimeout(this.props.dispatch, 'You just logged in.') 3 4// otherComponent.js 5showNotificationWithTimeout(this.props.dispatch, 'You just logged out.')

但是为什么showNotificationWithTimeout()要接收dispatch作为第一个参数呢?因为他需要将action发给store。一般组件是可以拿到dispatch的,为了让外部方法也能dispatch,我们需要给他dispath作为参数。

如果你有一个单例的store,你也可以让showNotificationWithTimeout直接引入这个store然后dispatch action

1// store.js 2export default createStore(reducer) 3 4// actions.js 5import store from './store' 6 7// ... 8 9let nextNotificationId = 0 10export function showNotificationWithTimeout(text) { 11 const id = nextNotificationId++ 12 store.dispatch(showNotification(id, text)) 13 14 setTimeout(() => { 15 store.dispatch(hideNotification(id)) 16 }, 5000) 17} 18 19// component.js 20showNotificationWithTimeout('You just logged in.') 21 22// otherComponent.js 23showNotificationWithTimeout('You just logged out.')

这样做看起来不复杂,也能达到效果,**但是我们不推荐这种做法!**主要原因是你的store必须是单例的,这让Server Render实现起来很麻烦。在Server端,你会希望每个请求都有自己的store,比便于不同的用户可以拿到不同的预加载内容。

一个单例的store也让单元测试很难写。测试action creator的时候你很难mock store,因为他引用了一个具体的真实的store。你甚至不能从外部重置store状态。

所以从技术上来说,你可以从一个module导出单例的store,但是我们不鼓励这样做。除非你确定加肯定你以后都不会升级Server Render。所以我们还是回到前面一种方案吧:

1// actions.js 2 3// ... 4 5let nextNotificationId = 0 6export function showNotificationWithTimeout(dispatch, text) { 7 const id = nextNotificationId++ 8 dispatch(showNotification(id, text)) 9 10 setTimeout(() => { 11 dispatch(hideNotification(id)) 12 }, 5000) 13} 14 15// component.js 16showNotificationWithTimeout(this.props.dispatch, 'You just logged in.') 17 18// otherComponent.js 19showNotificationWithTimeout(this.props.dispatch, 'You just logged out.')

这个方案就可以解决重复代码和竞争问题。

Thunk中间件

对于简单项目,上面的方案应该已经可以满足需求了。

但是对于大型项目,你可能还是会觉得这样使用并不方便。

比如,似乎我们必须将dispatch作为参数传递,这让我们分隔容器组件和展示组件变得更困难,因为任何发出异步Redux action的组件都必须接收dispatch作为参数,这样他才能将它继续往下传。你也不能仅仅使用connect()来绑定action creator,因为showNotificationWithTimeout()并不是一个真正的action creator,他返回的也不是Redux action

还有个很尴尬的事情是,你必须记住哪个action cerator是同步的,比如showNotification,哪个是异步的辅助方法,比如showNotificationWithTimeout。这两个的用法是不一样的,你需要小心的不要传错了参数,也不要混淆了他们。

这就是我们为什么需要找到一个“合法”的方法给辅助方法提供dispatch参数,并且帮助Redux区分出哪些是异步的action creator,好特殊处理他们

如果你的项目中面临着类似的问题,欢迎使用Redux Thunk中间件。

简单来说,React Thunk告诉Redux怎么去区分这种特殊的action----他其实是个函数:

1import { createStore, applyMiddleware } from 'redux' 2import thunk from 'redux-thunk' 3 4const store = createStore( 5 reducer, 6 applyMiddleware(thunk) 7) 8 9// 这个是普通的纯对象action 10store.dispatch({ type: 'INCREMENT' }) 11 12// 但是有了Thunk,他就可以识别函数了 13store.dispatch(function (dispatch) { 14 // 这个函数里面又可以dispatch很多action 15 dispatch({ type: 'INCREMENT' }) 16 dispatch({ type: 'INCREMENT' }) 17 dispatch({ type: 'INCREMENT' }) 18 19 setTimeout(() => { 20 // 异步的dispatch也可以 21 dispatch({ type: 'DECREMENT' }) 22 }, 1000) 23}) 24

如果你使用了这个中间件,而且你dispatch的是一个函数,React Thunk会自己将dispatch作为参数传进去。而且他会将这些函数action“吃了”,所以不用担心你的reducer会接收到奇怪的函数参数。你的reducer只会接收到纯对象action,无论是直接发出的还是前面那些异步函数发出的。

这个看起来好像也没啥大用,对不对?在当前这个例子确实是的!但是他让我们可以像定义一个普通的action creator那样去定义showNotificationWithTimeout

1// actions.js 2function showNotification(id, text) { 3 return { type: 'SHOW_NOTIFICATION', id, text } 4} 5function hideNotification(id) { 6 return { type: 'HIDE_NOTIFICATION', id } 7} 8 9let nextNotificationId = 0 10export function showNotificationWithTimeout(text) { 11 return function (dispatch) { 12 const id = nextNotificationId++ 13 dispatch(showNotification(id, text)) 14 15 setTimeout(() => { 16 dispatch(hideNotification(id)) 17 }, 5000) 18 } 19}

注意这里的showNotificationWithTimeout跟我们前面的那个看起来非常像,但是他并不需要接收dispatch作为第一个参数。而是返回一个函数来接收dispatch作为第一个参数。

那在我们的组件中怎么使用这个函数呢,我们当然可以这样写:

1// component.js 2showNotificationWithTimeout('You just logged in.')(this.props.dispatch)

这样我们直接调用了异步的action creator来得到内层的函数,这个函数需要dispatch做为参数,所以我们给了他dispatch参数。

然而这样使用岂不是更尬,还不如我们之前那个版本的!我们为啥要这么干呢?

我之前就告诉过你:只要使用了Redux Thunk,如果你想dispatch一个函数,而不是一个纯对象,这个中间件会自己帮你调用这个函数,而且会将dispatch作为第一个参数传进去。

所以我们可以直接这样干:

1// component.js 2this.props.dispatch(showNotificationWithTimeout('You just logged in.'))

最后,对于组件来说,dispatch一个异步的action(其实是一堆普通action)看起来和dispatch一个普通的同步action看起来并没有啥区别。这是个好现象,因为组件就不应该关心那些动作到底是同步的还是异步的,我们已经将它抽象出来了。

注意因为我们已经教了Redux怎么区分这些特殊的action creator(我们称之为thunk action creator),现在我们可以在任何普通的action creator的地方使用他们了。比如,我们可以直接在connect()中使用他们:

1// actions.js 2 3function showNotification(id, text) { 4 return { type: 'SHOW_NOTIFICATION', id, text } 5} 6function hideNotification(id) { 7 return { type: 'HIDE_NOTIFICATION', id } 8} 9 10let nextNotificationId = 0 11export function showNotificationWithTimeout(text) { 12 return function (dispatch) { 13 const id = nextNotificationId++ 14 dispatch(showNotification(id, text)) 15 16 setTimeout(() => { 17 dispatch(hideNotification(id)) 18 }, 5000) 19 } 20} 21 22// component.js 23 24import { connect } from 'react-redux' 25 26// ... 27 28this.props.showNotificationWithTimeout('You just logged in.') 29 30// ... 31 32export default connect( 33 mapStateToProps, 34 { showNotificationWithTimeout } 35)(MyComponent)

在Thunk中读取State

通常来说,你的reducer会包含计算新的state的逻辑,但是reducer只有当你dispatchaction才会触发。如果你在thunk action creator中有一个副作用(比如一个API调用),某些情况下,你不想发出这个action该怎么办呢?

如果没有Thunk中间件,你需要在组件中添加这个逻辑:

1// component.js 2if (this.props.areNotificationsEnabled) { 3 showNotificationWithTimeout(this.props.dispatch, 'You just logged in.') 4}

但是我们提取action creator的目的就是为了集中这些在各个组件中重复的逻辑。幸运的是,Redux Thunk提供了一个读取当前store state的方法。那就是除了传入dispatch参数外,他还会传入getState作为第二个参数,这样thunk就可以读取store的当前状态了。

1let nextNotificationId = 0 2export function showNotificationWithTimeout(text) { 3 return function (dispatch, getState) { 4 // 不像普通的action cerator,这里我们可以提前退出 5 // Redux不关心这里的返回值,没返回值也没关系 6 if (!getState().areNotificationsEnabled) { 7 return 8 } 9 10 const id = nextNotificationId++ 11 dispatch(showNotification(id, text)) 12 13 setTimeout(() => { 14 dispatch(hideNotification(id)) 15 }, 5000) 16 } 17}

但是不要滥用这种方法!如果你需要通过检查缓存来判断是否发起API请求,这种方法就很好,但是将你整个APP的逻辑都构建在这个基础上并不是很好。如果你只是用getState来做条件判断是否要dispatch action,你可以考虑将这些逻辑放到reducer里面去。

下一步

现在你应该对thunk的工作原理有了一个基本的概念,如果你需要更多的例子,可以看这里:https://redux.js.org/introduction/examples#async

你可能会发现很多例子都返回了Promise,这个不是必须的,但是用起来却很方便。Redux并不关心你的thunk返回了什么值,但是他会将这个值通过外层的dispatch()返回给你。这就是为什么你可以在thunk中返回一个Promise并且等他完成:

dispatch(someThunkReturningPromise()).then(...)

另外你还可以将一个复杂的thunk action creator拆分成几个更小的thunk action creator。这是因为thunk提供的dispatch也可以接收thunk,所以你可以一直嵌套的dispatch thunk。而且结合Promise的话可以更好的控制异步流程。

在一些更复杂的应用中,你可能会发现你的异步控制流程通过thunk很难表达。比如,重试失败的请求,使用token进行重新授权认证,或者在一步一步的引导流程中,使用这种方式可能会很繁琐,而且容易出错。如果你有这些需求,你可以考虑下一些更高级的异步流程控制库,比如Redux Saga或者Redux Loop。可以看看他们,评估下,哪个更适合你的需求,选一个你最喜欢的。

最后,不要使用任何库(包括thunk)如果你没有真实的需求。记住,我们的实现都是要看需求的,也许你的需求这个简单的方案就能满足:

1store.dispatch({ type: 'SHOW_NOTIFICATION', text: 'You logged in.' }) 2setTimeout(() => { 3 store.dispatch({ type: 'HIDE_NOTIFICATION' }) 4}, 5000)

不要跟风尝试,除非你知道你为什么需要这个!

----翻译到此结束----

StackOverflow的大神**Dan Abramov**对这个问题的回答实在太细致,太到位了,以致于我看了之后都不敢再写这个原因了,以此翻译向大神致敬,再贴下这个回答的地址:https://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559

PS: Dan Abramov是Redux生态的核心作者,这几篇文章讲的ReduxReact-ReduxRedux-Thunk都是他的作品。

源码解析

上面关于原因的翻译其实已经将Redux适用的场景和原理讲的很清楚了,下面我们来看看他的源码,自己仿写一个来替换他。照例我们先来分析下要点:

  1. Redux-Thunk是一个Redux中间件,所以他遵守Redux中间件的范式。
  2. thunk是一个可以dispatch的函数,所以我们需要改写dispatch让他接受函数参数。

Redux中间件范式

在我前面那篇讲Redux源码的文章讲过中间件的范式以及Redux中这块源码是怎么实现的,没看过或者忘了的朋友可以再去看看。我这里再简单提一下,一个Redux中间件结构大概是这样:

1function logger(store) { 2 return function(next) { 3 return function(action) { 4 console.group(action.type); 5 console.info('dispatching', action); 6 let result = next(action); 7 console.log('next state', store.getState()); 8 console.groupEnd(); 9 return result 10 } 11 } 12}

这里注意几个要点:

  1. 一个中间件接收store作为参数,会返回一个函数
  2. 返回的这个函数接收老的dispatch函数作为参数(也就是代码中的next),会返回一个新的函数
  3. 返回的新函数就是新的dispatch函数,这个函数里面可以拿到外面两层传进来的store和老dispatch函数

仿照这个范式,我们来写一下thunk中间件的结构:

1function thunk(store) { 2 return function (next) { 3 return function (action) { 4 // 先直接返回原始结果 5 let result = next(action); 6 return result 7 } 8 } 9}

处理thunk

根据我们前面讲的,thunk是一个函数,接收dispatch getState两个参数,所以我们应该将thunk拿出来运行,然后给他传入这两个参数,再将它的返回值直接返回就行。

1function thunk(store) { 2 return function (next) { 3 return function (action) { 4 // 从store中解构出dispatch, getState 5 const { dispatch, getState } = store; 6 7 // 如果action是函数,将它拿出来运行,参数就是dispatch和getState 8 if (typeof action === 'function') { 9 return action(dispatch, getState); 10 } 11 12 // 否则按照普通action处理 13 let result = next(action); 14 return result 15 } 16 } 17}

接收额外参数withExtraArgument

Redux-Thunk还提供了一个API,就是你在使用applyMiddleware引入的时候,可以使用withExtraArgument注入几个自定义的参数,比如这样:

1const api = "http://www.example.com/sandwiches/"; 2const whatever = 42; 3 4const store = createStore( 5 reducer, 6 applyMiddleware(thunk.withExtraArgument({ api, whatever })), 7); 8 9function fetchUser(id) { 10 return (dispatch, getState, { api, whatever }) => { 11 // 现在你可以使用这个额外的参数api和whatever了 12 }; 13}

这个功能要实现起来也很简单,在前面的thunk函数外面再包一层就行:

1// 外面再包一层函数createThunkMiddleware接收额外的参数 2function createThunkMiddleware(extraArgument) { 3 return function thunk(store) { 4 return function (next) { 5 return function (action) { 6 const { dispatch, getState } = store; 7 8 if (typeof action === 'function') { 9 // 这里执行函数时,传入extraArgument 10 return action(dispatch, getState, extraArgument); 11 } 12 13 let result = next(action); 14 return result 15 } 16 } 17 } 18}

然后我们的thunk中间件其实相当于没传extraArgument

const thunk = createThunkMiddleware();

而暴露给外面的withExtraArgument函数就直接是createThunkMiddleware了:

thunk.withExtraArgument = createThunkMiddleware;

源码解析到此结束。啥,这就完了?是的,这就完了!Redux-Thunk就是这么简单,虽然背后的思想比较复杂,但是代码真的只有14行!我当时也震惊了,来看看官方源码吧:

1function createThunkMiddleware(extraArgument) { 2 return ({ dispatch, getState }) => (next) => (action) => { 3 if (typeof action === 'function') { 4 return action(dispatch, getState, extraArgument); 5 } 6 7 return next(action); 8 }; 9} 10 11const thunk = createThunkMiddleware(); 12thunk.withExtraArgument = createThunkMiddleware; 13 14export default thunk;

总结

  1. 如果说Redux是“百行代码,千行文档”,那Redux-Thunk就是“十行代码,百行思想”。
  2. Redux-Thunk最主要的作用是帮你给异步action传入dispatch,这样你就不用从调用的地方手动传入dispatch,从而实现了调用的地方和使用的地方的解耦。
  3. ReduxRedux-Thunk让我深深体会到什么叫“编程思想”,编程思想可以很复杂,但是实现可能并不复杂,但是却非常有用。
  4. 在我们评估是否要引入一个库时最好想清楚我们为什么要引入这个库,是否有更简单的方案。

本文手写代码已经上传GitHub,大家可以拿下来玩玩:https://github.com/dennis-jiang/Front-End-Knowledges/blob/master/Examples/React/redux-thunk/src/myThunk.js

参考资料

Redux-Thunk文档:https://github.com/reduxjs/redux-thunk

Redux-Thunk源码: https://github.com/reduxjs/redux-thunk/blob/master/src/index.js

Dan Abramov在StackOverflow上的回答: https://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559

文章的最后,感谢你花费宝贵的时间阅读本文,如果本文给了你一点点帮助或者启发,请不要吝啬你的赞和GitHub小星星,你的支持是作者持续创作的动力。

作者博文GitHub项目地址: https://github.com/dennis-jiang/Front-End-Knowledges

作者掘金文章汇总:https://juejin.im/post/5e3ffc85518825494e2772fd

我也搞了个公众号[进击的大前端],不打广告,不写水文,只发高质量原创,欢迎关注~

QRCode

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )