首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应本机-无法读取未定义的属性[属性](点燃2,还原沙司)

反应本机-无法读取未定义的属性[属性](点燃2,还原沙司)
EN

Stack Overflow用户
提问于 2017-08-01 00:28:06
回答 1查看 750关注 0票数 1

在我的react本地应用程序中使用redux是有困难的。我不能在我的组件中调用一个动作。我得到以下错误:

这是我的AuthRedux.js

代码语言:javascript
复制
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'

const { Types, Creators } = createActions({
    login: ['email', 'password'],
    logout: null
})

export const AuthTypes = Types
export default Creators

export const INITIAL_STATE = Immutable({
    isLoggedIn: false,
    email: null,
    password: null
})

export const userLogin = (state, {email, password}) => {
    return Object.assign({}, state, {
        isLoggedIn: true
    });//state.merge({ isLoggedIn: true, email, password})
}

export const userLogout = (state) => {
    return state.merge({ isLoggedIn: false, email: null, password: null })
}

export const reducer = createReducer(INITIAL_STATE, {
    [Types.USER_LOGIN]: userLogin,
    [Types.USER_LOGOUT]: userLogout
})

这是我的组件LoginScreen.js

代码语言:javascript
复制
import React, { Component } from 'react'
import { ScrollView, Text, KeyboardAvoidingView, TextInput, TouchableOpacity, Button } from 'react-native'
import { connect } from 'react-redux'
import { AuthActions } from '../Redux/AuthRedux'
// Add Actions - replace 'Your' with whatever your reducer is called :)
// import YourActions from '../Redux/YourRedux'

// Styles
import styles from './Styles/LoginScreenStyle'

class LoginScreen extends Component {

  constructor(props) {
    super(props);
    this.state = {
      email: '',
      password: '',
      opacity: 1.0,
      isLoggedIn: false
    }
  }

  render () {
    return (
      <ScrollView style={styles.container}>
        <KeyboardAvoidingView behavior='position'>
          <Text>LoginScreen</Text>
          <TextInput style={{width: 100, backgroundColor: 'red', height: 50, marginTop: 10}} onChangeText={(text) => this.setState({email : text})}/>
          <TextInput style={{width: 100, backgroundColor: 'yellow', height: 50, marginTop: 10}} onChangeText={(text) => this.setState({password : text})}/>
          <Button title='Hola' onPress={this.onLogin}/>
        </KeyboardAvoidingView>
      </ScrollView>
    )
  }

  onLogin = () => {
    console.log(this.state.email);
    this.setState({opacity: 0.5})
    this.props.userLogin(this.state.email, this.state.password);
  }

  handleOnPress = () => {
    this.setState({opacity: 0.5})
  }
}

const mapStateToProps = (state) => {
  return {
    isLoggedIn: state.auth.isLoggedIn
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    userLogin: (email, password) => dispatch(AuthActions.login(email, password))
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(LoginScreen)

我正在尝试从userLogin按钮调用mapDispatchToProps函数,该按钮在mapDispatchToProps中分配。我的rootReducer也是这样配置的:

代码语言:javascript
复制
const rootReducer = combineReducers({
    nav: require('./NavigationRedux').reducer,
    github: require('./GithubRedux').reducer,
    search: require('./SearchRedux').reducer,
    auth: require('./AuthRedux').reducer
  })

在App.js中,该商店也提供给供应商。

代码语言:javascript
复制
class App extends Component {
  render () {
    return (
      <Provider store={store}>
        <RootContainer />
      </Provider>
    )
  }
}

我不知道为什么没有检测到登录操作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-01 01:24:58

不要使用import { AuthActions } from '../Redux/AuthRedux',而要执行import AuthActions from '../Redux/AuthRedux',因为您要在actionCreators上执行export default,这是您现在要导入的。

您还可以在执行export const AuthActions = Creators的地方执行export default Creators,并且可以按照现在的方式保持import语句不变。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45427186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档