【问题标题】:Keep getting 'Object is not a function' error不断收到“对象不是函数”错误
【发布时间】:2018-11-28 00:29:41
【问题描述】:

我的 Main.js 文件中不断收到错误消息“对象不是函数...”。如何修复它以及我做错了什么?这是错误的屏幕截图:。这也是我的 Main.js 文件的代码:

import React from 'react';
import { StyleSheet, Text, Image, View, Vibrate } from 'react-native';

import Settings from './Settings';
import Profile from './Profile';

import {connect} from 'react-redux';
import {saveProfile, saveSettings} from '../redux/actions';

class Main extends React.Component {

  componentWillMount() {
        if (!this.props.avatarUrl) {
            this.props.saveProfile(this.props.location);
        }
    }

  render() {
    return (
      <View style={styles.container, bgColor}>
        <Image
          source={this.props.avatarUrl}
          style={{width: 300, height: 300}}
        />
        <Text>{this.props.name}</Text>
        <Text>{this.props.phone}</Text>
        <Text>{this.props.email}</Text>
        <Settings />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default saveProfile (
  connect(
    state=>state.location,
    {saveProfile}
  )
)(Main);

我的目标是显示redux中保存的信息。

【问题讨论】:

  • 你试过connect( state=&gt;state.location, {saveProfile} )(saveProfile (Main))吗?
  • 它给出了语法错误SyntaxError: /Users/arina/Desktop/mobapp/comps/Main.js: Unexpected token, expected "," (47:20)
  • 在您的 connect(...) 中,请将 {saveProfile} 替换为 null。现在可以编译了吗?
  • 把里面的saveProfile也去掉,所以export default connect( state=&gt;state.location, null )(Main)
  • 在这种情况下,您可以省略第二个参数,而只需 connect( state=&gt;state.location)(Main)

标签: reactjs react-native redux react-redux redux-thunk


【解决方案1】:

你的问题是你导出你的主模块的方式和你连接redux的方式来使用props,我给你下面的例子让你了解redux的使用...... `

actions.js

import * as ACTIONS from './types';

export const reset = () => {
  return { type: ACTIONS.RESET_STATE };
};

export const saveProfile = profile => {
  return { type: ACTIONS.SAVE_PROFILE, payload: profile };
};

export const saveSettings = settings => {
  return { type: ACTIONS.SAVE_SETTINGS, payload: settings };
};

reducer.js

import * as ACTIONS from './../actions/types';

const INITIAL_STATE = {
  profile: 0,
  settings: 0
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ACTIONS.RESET_STATE:
      return { ...INITIAL_STATE };
    case ACTIONS.SAVE_PROFILE:
      return { ...state, profile: action.profile };
    case ACTIONS.SAVE_SETTINGS:
      return { ...state, settings: action.settings };
    default:
      return state;
  }
};

index.js /combinerreducers

import { combineReducers } from 'redux';

import reducers from './reducer.js';

export default combineReducers({
  reducer: reducers
});

并在你的主类中使用

import { saveProfile, saveSettings } from './../redux/actions/index.js';

class Main extends Component {
  constructor(props) {
    super(props);
    this.state = {
      saveProfile: this.props.profile,
      saveSettings: this.props.settings
    };
{.....



.....}

const mapStateToProps = ({ reducers }) => {
  const { profile, settings } = reducers;

  return { profile, settings };
};

export default connect(mapStateToProps, { saveProfile, saveSettings })(Main);

希望对你有帮助

【讨论】:

    【解决方案2】:

    看起来错误来自底部的导出默认语句。此特定导出看起来可能存在语法错误,因此请仔细检查您使用的示例中的结构。

    【讨论】:

    • 我照原样按照示例进行操作,但它不适合我。
    猜你喜欢
    • 2021-09-29
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多