gpt4 book ai didi

javascript - React-redux @connect 不起作用,但 connect() 起作用

转载 作者:行者123 更新时间:2023-11-29 17:51:39 26 4
gpt4 key购买 nike

我无法使用 @connect() 语法代替 export default connect()

我注意到当我使用通常的语法时

class PhonePage extends Component { ... }

export default connect(state => ({
testedByPhone: state.phonepage.testedByPhone
}),
(dispatch) => ({
actions: bindActionCreators(testUserPhone, dispatch)
})
)(PhonePage)

我在商店中正确注册了我的州。

它看起来像这样:Object {screenProps: undefined, navigation: Object, testedByPhone: Object}

但是当我使用@connect 装饰器使事情变得更清晰时,我的状态中没有列出任何内容。

@connect(
state => ({
testedByPhone: state.phonepage.testedByPhone
}),
{ testUserPhone }
)
class PhonePage extends Component { ... }

export default PhonePage

突然间,它实际上并没有真正连接事物:Object {screenProps: undefined, navigation: Object}

我做错了什么,我看到每个人都在使用神奇的@connect 装饰器的正确使用方法是什么?


其余代码,以备不时之需,采用@connect的形式;

'use strict'

import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
ScrollView,
View,
StatusBar,
TouchableHighlight,
Button,
TextInput
} from 'react-native'
import { NavigationActions } from 'react-navigation'

import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { testUserPhone } from './actions'

import PhoneInput from 'react-native-phone-input'

@connect(
state => ({
testedByPhone: state.phonepage.testedByPhone
}),
{ testUserPhone }
)
class PhonePage extends Component {

constructor(){
super()
}

componentDidMount() {
// this.props.testUserPhone
}

render() {
console.log(this.props)
return(
...
)
}
}

export default PhonePage

// export default connect(state => ({
// testedByPhone: state.phonepage.testedByPhone
// }),
// (dispatch) => ({
// actions: bindActionCreators(testUserPhone, dispatch)
// })
// )(PhonePage)

// module.exports = PhonePage

最佳答案

@connect 装饰器,或者一般的装饰器,不是 JavaScript 原生的。但是,您可以通过 Babel 添加它们。

您可以阅读更多内容 here

npm install --save-dev babel-plugin-transform-decorators-legacy

关于javascript - React-redux @connect 不起作用,但 connect() 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43023175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com