不断收到 'Object is not a function' 错误
Keep getting 'Object is not a function' error
我的 Main.js 文件一直收到错误 'Object is not a function ...'。如何修复它以及我做错了什么?这是错误的屏幕截图:。这也是我的 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中保存的信息。
看起来错误来自底部的 export default 语句。这个特定的导出看起来可能有语法错误,因此请仔细检查您正在使用的示例中的结构。
你的问题是你导出主模块的方式和你连接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 /combinereducers
import { combineReducers } from 'redux';
import reducers from './reducer.js';
export default combineReducers({
reducer: reducers
});
和你的用法 Main class
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);
希望对你有帮助
我的 Main.js 文件一直收到错误 'Object is not a function ...'。如何修复它以及我做错了什么?这是错误的屏幕截图:
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中保存的信息。
看起来错误来自底部的 export default 语句。这个特定的导出看起来可能有语法错误,因此请仔细检查您正在使用的示例中的结构。
你的问题是你导出主模块的方式和你连接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 /combinereducers
import { combineReducers } from 'redux';
import reducers from './reducer.js';
export default combineReducers({
reducer: reducers
});
和你的用法 Main class
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);
希望对你有帮助