react-native組件綁定了redux導致通過ref無法獲取到組件內(nèi)部方法

現(xiàn)象說明:我們在組件的相互通信時,有時候會用到通過ref調(diào)用組件內(nèi)部方法,在使用時發(fā)現(xiàn)在有些時候通過ref可以獲取到該節(jié)點,卻無法獲取到內(nèi)部的自定義方法。

先說解決辦法

網(wǎng)上看到的解決辦法坏瘩,實測可以解決,(已過時漠魏,至少react-redux 7.1.3會報錯)

更新時間2019年/12月/11日

最近使用react-native發(fā)現(xiàn)還按照下面的方法使用時倔矾,會報錯。
react-native:0.59.1
react-redux: 7.1.3

*** withRef is removed. To access the wrapped instance, use a ref on the connected component ***
先貼上解決辦法:

// 最原始的導出一個綁定到redux的組件 柱锹,無法導出內(nèi)部屬性
// export default connect(mapStateToProps, mapActionToProps)(App);
// 以前版本可以導出內(nèi)部屬性的改動
export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(App);
// 最新react-redux可以導出內(nèi)部屬性的改動
export default connect(mapStateToProps, mapActionToProps, null, {forwardRef: true})(App);

看著只是一個key值的改動哪自。
但最下面的方法我之前是試過的,所以肯定是因為react-redux插件版本不一樣導致的報錯禁熏。
查看connect函數(shù)源碼壤巷,點擊connect函數(shù)。

export const connect: Connect;
...
export interface Connect {
    // tslint:disable:no-unnecessary-generics
    (): InferableComponentEnhancer<DispatchProp>;

    <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>
    ): InferableComponentEnhancerWithProps<TStateProps & DispatchProp, TOwnProps>;

    <no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
        mapStateToProps: null | undefined,
        mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;

    <no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
        mapStateToProps: null | undefined,
        mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
    ): InferableComponentEnhancerWithProps<
        ResolveThunks<TDispatchProps>,
        TOwnProps
    >;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
    ): InferableComponentEnhancerWithProps<
        TStateProps & ResolveThunks<TDispatchProps>,
        TOwnProps
    >;

    <no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
        mapStateToProps: null | undefined,
        mapDispatchToProps: null | undefined,
        mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
    ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

    <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: null | undefined,
        mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
    ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

    <no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
        mapStateToProps: null | undefined,
        mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
        mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
    ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

    <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: null | undefined,
        mergeProps: null | undefined,
        options: Options<State, TStateProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<DispatchProp & TStateProps, TOwnProps>;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
        mapStateToProps: null | undefined,
        mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>,
        mergeProps: null | undefined,
        options: Options<{}, TStateProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
        mapStateToProps: null | undefined,
        mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
        mergeProps: null | undefined,
        options: Options<{}, TStateProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<
        ResolveThunks<TDispatchProps>,
        TOwnProps
    >;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>,
        mergeProps: null | undefined,
        options: Options<State, TStateProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
        mergeProps: null | undefined,
        options: Options<State, TStateProps, TOwnProps>
    ): InferableComponentEnhancerWithProps<
        TStateProps & ResolveThunks<TDispatchProps>,
        TOwnProps
    >;

    <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}, State = {}>(
        mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
        mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
        mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
        options?: Options<State, TStateProps, TOwnProps, TMergedProps>
    ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
    // tslint:enable:no-unnecessary-generics
}
// 我們看到這些個函數(shù)瞧毙,不同的入?yún)㈦驶N覀冎灰磑ptions就可以。
// options指向 Options
...
export interface Options<State = {}, TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
    /**
     * If true, implements shouldComponentUpdate and shallowly compares the result of mergeProps,
     * preventing unnecessary updates, assuming that the component is a “pure” component
     * and does not rely on any input or state other than its props and the selected Redux store’s state.
     * Defaults to true.
     * @default true
     */
    pure?: boolean;

    /**
     * When pure, compares incoming store state to its previous value.
     * @default strictEqual
     */
    areStatesEqual?: (nextState: State, prevState: State) => boolean;

    /**
     * When pure, compares incoming props to its previous value.
     * @default shallowEqual
     */
    areOwnPropsEqual?: (nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean;

    /**
     * When pure, compares the result of mapStateToProps to its previous value.
     * @default shallowEqual
     */
    areStatePropsEqual?: (nextStateProps: TStateProps, prevStateProps: TStateProps) => boolean;

    /**
     * When pure, compares the result of mergeProps to its previous value.
     * @default shallowEqual
     */
    areMergedPropsEqual?: (nextMergedProps: TMergedProps, prevMergedProps: TMergedProps) => boolean;

    /**
     * If true, use React's forwardRef to expose a ref of the wrapped component
     *
     * @default false
     */
    forwardRef?: boolean;
}

看上面的源碼我們就能看到 forwardRef這個了宙彪。
其余的以后再將矩动。


發(fā)現(xiàn)問題過程

經(jīng)過與能正常使用ref的組件對比發(fā)現(xiàn)是以下代碼導致的

const mapStateToProps =(state) => ({
  login: state.login,
});
const mapActionToProps =(dispatch) => ({
  loginAction: bindActionCreators(LoginAction, dispatch),
});

// export default connect(mapStateToProps, mapActionToProps)(App);

// 修改為下行代碼就可以通過ref獲取到內(nèi)部函數(shù)
export default connect(mapStateToProps, mapActionToProps, null, {withRef: true})(App);

應該是connect函數(shù)導致export的組件的問題。
connect 是從 react-redux 庫中引入

查看connect函數(shù)代碼

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末释漆,一起剝皮案震驚了整個濱河市悲没,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌男图,老刑警劉巖示姿,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異逊笆,居然都是意外死亡峻凫,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門览露,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人譬胎,你說我怎么就攤上這事差牛∶” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵偏化,是天一觀的道長脐恩。 經(jīng)常有香客問我,道長侦讨,這世上最難降的妖魔是什么驶冒? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮韵卤,結(jié)果婚禮上骗污,老公的妹妹穿的比我還像新娘。我一直安慰自己沈条,他們只是感情好需忿,可當我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蜡歹,像睡著了一般屋厘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上月而,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天汗洒,我揣著相機與錄音,去河邊找鬼父款。 笑死溢谤,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的铛漓。 我是一名探鬼主播溯香,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼浓恶!你這毒婦竟也來了玫坛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤包晰,失蹤者是張志新(化名)和其女友劉穎湿镀,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體伐憾,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡勉痴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了树肃。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒸矛。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出雏掠,到底是詐尸還是另有隱情斩祭,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布乡话,位于F島的核電站摧玫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏绑青。R本人自食惡果不足惜诬像,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望闸婴。 院中可真熱鬧坏挠,春花似錦、人聲如沸掠拳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽溺欧。三九已至喊熟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間姐刁,已是汗流浹背芥牌。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留聂使,地道東北人壁拉。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像柏靶,于是被迫代替她去往敵國和親弃理。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,072評論 25 707
  • 用兩張圖告訴你屎蜓,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料痘昌? 從這篇文章中你...
    hw1212閱讀 12,714評論 2 59
  • Who Are You? 你是誰? You Are Every Single Day. 你是生命中經(jīng)歷的每一天炬转。 ...
    心一Sue閱讀 2,283評論 3 8
  • 今天是媽媽的周年日辆苔,回家給媽媽燒周年下午由于身體原因去醫(yī)院動個小手術,現(xiàn)在躺在床上寫日記扼劈,孩子回來看我躺著不舒服問...
    相王飛閱讀 116評論 0 1
  • 夜雨寄北 李商隱君問歸期未有期荐吵,巴山夜雨漲秋池骑冗。何當共剪西窗燭赊瞬,卻話巴山夜雨時。 這首詩...
    藍蝶landie閱讀 893評論 3 4