Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(RN): #584 添加 taro-redux-rn 包,重构 taro-router-rn。
- Loading branch information
1 parent
0cee9ca
commit b8ab4d7
Showing
6 changed files
with
201 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@tarojs/taro-redux-rn", | ||
"version": "1.0.0-beta.26", | ||
"description": "taro-redux-rn", | ||
"main": "./src/index.js", | ||
"files": [ | ||
"src", | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/NervJS/taro.git" | ||
}, | ||
"keywords": [ | ||
"router" | ||
], | ||
"author": "O2Team", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"react": "16.3.0" | ||
}, | ||
"dependencies": { | ||
"react-redux": "^5.0.7" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Provider, connect as originConnect, connectAdvanced } from 'react-redux' | ||
import React from 'react' | ||
|
||
function connect (mapStateToProps = null, mapDispatchToProps = null, mergeProps = null, options = {}) { | ||
|
||
options.withRef = true | ||
let connectAdvanced = originConnect(mapStateToProps, mapDispatchToProps, mergeProps, options) | ||
|
||
return (WrappedComponent) => { | ||
let Connect = connectAdvanced(WrappedComponent) | ||
|
||
class WrappedConnect extends React.Component { | ||
constructor (props, context) { | ||
super(props, context) | ||
this.connectRef = React.createRef() | ||
} | ||
|
||
componentDidMount () { | ||
let wrappedInstance = this.connectRef.current.getWrappedInstance() | ||
wrappedInstance.componentDidShow && wrappedInstance.componentDidShow() | ||
} | ||
|
||
componentWillUnmount () { | ||
let wrappedInstance = this.connectRef.current.getWrappedInstance() | ||
wrappedInstance.componentDidHide && wrappedInstance.componentDidHide() | ||
} | ||
|
||
render () { | ||
return ( | ||
<Connect ref={this.connectRef} {...this.props}/> | ||
) | ||
} | ||
} | ||
|
||
return WrappedConnect | ||
} | ||
} | ||
|
||
export { Provider, connect, connectAdvanced } | ||
export default {Provider, connect, connectAdvanced} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React from 'react' | ||
import queryString from 'query-string' | ||
import RefreshProvider from './RefreshProvider' | ||
|
||
class TaroProvider extends React.Component { | ||
constructor (props, context) { | ||
super(props, context) | ||
this.refreshProviderRef = React.createRef() | ||
let {Taro} = this.props | ||
// 这样处理不一定合理, | ||
// 有时间看一下 react-navigation 内部的实现机制再优化 | ||
Taro.navigateTo = this.wxNavigateTo.bind(this) | ||
Taro.redirectTo = this.wxRedirectTo.bind(this) | ||
Taro.navigateBack = this.wxNavigateBack.bind(this) | ||
Taro.switchTab = this.wxSwitchTab.bind(this) | ||
Taro.getCurrentPages = this.wxGetCurrentPages.bind(this) | ||
} | ||
|
||
componentDidMount () { | ||
let {Taro} = this.props | ||
try { | ||
Taro.startPullDownRefresh = this.refreshProviderRef.current && this.refreshProviderRef.current.handlePullDownRefresh | ||
Taro.stopPullDownRefresh = this.refreshProviderRef.current && this.refreshProviderRef.current.stopPullDownRefresh | ||
} catch (e) { | ||
console.log('this.refreshProviderRef: ') | ||
console.log(this.refreshProviderRef) | ||
throw e | ||
} | ||
} | ||
|
||
wxNavigateTo ({url, success, fail, complete}) { | ||
if (url.startsWith('/')) { | ||
url = url.substr(1) | ||
} | ||
|
||
let obj = queryString.parseUrl(url) | ||
console.log(obj) | ||
try { | ||
this.props.navigation.push(obj.url, obj.query) | ||
} catch (e) { | ||
fail && fail(e) | ||
complete && complete(e) | ||
throw e | ||
} | ||
success && success() | ||
complete && complete() | ||
} | ||
|
||
wxRedirectTo ({url, success, fail, complete}) { | ||
if (url.startsWith('/')) { | ||
url = url.substr(1) | ||
} | ||
|
||
let obj = queryString.parseUrl(url) | ||
console.log(obj) | ||
try { | ||
this.props.navigation.replace(obj.url, obj.query) | ||
} catch (e) { | ||
fail && fail(e) | ||
complete && complete(e) | ||
throw e | ||
} | ||
success && success() | ||
complete && complete() | ||
} | ||
|
||
wxSwitchTab ({url, success, fail, complete}) { | ||
if (url.startsWith('/')) { | ||
url = url.substr(1) | ||
} | ||
|
||
let obj = queryString.parseUrl(url) | ||
console.log(obj) | ||
try { | ||
this.props.navigation.navigate(obj.url, obj.query) | ||
} catch (e) { | ||
fail && fail(e) | ||
complete && complete(e) | ||
throw e | ||
} | ||
success && success() | ||
complete && complete() | ||
} | ||
|
||
wxNavigateBack ({delta = 1}) { | ||
while (delta > 0) { | ||
this.props.navigation.goBack() | ||
delta-- | ||
} | ||
} | ||
|
||
wxGetCurrentPages () { | ||
let parentState = this.props.navigation.dangerouslyGetParent().state | ||
if (parentState && parentState.routes) { | ||
return parentState.routes.map(item => item.routeName) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
render () { | ||
let {enablePullDownRefresh, navigationOptions} = this.props | ||
let isScreenEnablePullDownRefresh = enablePullDownRefresh || (navigationOptions && navigationOptions.enablePullDownRefresh) | ||
return ( | ||
<RefreshProvider | ||
enablePullDownRefresh={isScreenEnablePullDownRefresh} | ||
onPullDownRefresh={this.onPullDownRefresh && this.onPullDownRefresh.bind(this)} | ||
onReachBottom={this.onReachBottom && this.onReachBottom.bind(this)} | ||
onScroll={this.onScroll && this.onScroll.bind(this)} | ||
ref={this.refreshProviderRef} | ||
> | ||
{this.props.children} | ||
</RefreshProvider> | ||
) | ||
} | ||
} | ||
|
||
export default TaroProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters