React-redux @connect 不起作用,但 connect() 起作用
React-redux @connect does not work, but connect() does
我无法使用 @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 添加它们。
您可以阅读更多内容
npm install --save-dev babel-plugin-transform-decorators-legacy
关于 babel-6,关注@megaboy101 的回答
对于 babel-7 使用这个
{
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
]
}
我无法使用 @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 添加它们。
您可以阅读更多内容
npm install --save-dev babel-plugin-transform-decorators-legacy
关于 babel-6,关注@megaboy101 的回答
对于 babel-7 使用这个
{
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
]
}