gpt4 book ai didi

reactjs - 连接 redux 功能不适用于 react native

转载 作者:行者123 更新时间:2023-12-03 13:30:03 24 4
gpt4 key购买 nike

我正在尝试使用react-native配置redux,当尝试从我的组件调用操作时,我遇到了一个名为“undefined is not a function”的错误,看来 action() 函数没有正确调用,我无法将状态作为表单组件内的 prop 访问。
这是我的代码:

index.android.js

  import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
// import ReduxThunk from 'redux-thunk';
import reducers from './src/reducers';
import { Form } from './src/components/Form';
import { AppRegistry } from 'react-native';

export class App extends Component {

componentWillMount() {
//here we will handle firebase sutup process for the authntication purpose .
}

render() {
const store = createStore(reducers);
return (
<Provider store={store}>
<Form />
</Provider>
);
}
}

const styles = {
main: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-start',
}
};

AppRegistry.registerComponent('test', () => App);

Form.js

 import React, { Component } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { Image } from "react-native";
import { emailChanged } from "../actions";
import {
Container,
ContainerSection,
Button,
Input
} from ".././components/common";

export class Form extends Component {
onEmailChange(text) {
//call the action creator for the email
this.props.emailChanged(text);
}

onPasswordChange(text) {
//call the action creator for the password
}

onButtonPress() {
//do something
}

renderButton() {
return <Button onPress={this.onButtonPress.bind(this)}>Log in</Button>;
}

render() {
return (
<Container>
<ContainerSection>
<Image source={require("../../images/logo.png")} />
</ContainerSection>
<ContainerSection>
{/* email input */}
<Input
label="Email"
placeholder="email@gmail.com"
value={this.props.email}
onChangeText={this.onEmailChange.bind(this)}
keyboardType="email-address"
/>
</ContainerSection>
<ContainerSection>
{/* password input */}
<Input
label="password"
placeholder="password"
value={this.props.password}
onChangeText={this.onPasswordChange.bind(this)}
secure
/>
</ContainerSection>
<ContainerSection>{this.renderButton()}</ContainerSection>
</Container>
);
}
}

const mapStateToProps = state => {
return {
email: state.auth.email,
password: state.auth.password
};
};

function mapDispatchToProps(dispatch) {
return bindActionCreators({ emailChanged }, dispatch);
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(Form);

index.js(在 actions 文件夹内)

    import { EMAIL_CHANGED, PASSWORD_CHANGED } from './types';
//it is an action creator
export const emailChanged = (text) => {
return {
type: EMAIL_CHANGED,
payload: text
};
};

//it is an action creator
export const passwordChanged = (text) => {
return {
type: PASSWORD_CHANGED,
payload: text
};
};

index.js(在reducers文件夹内)

import { combineReducers } from 'redux';
import AuthReducer from './AuthReducer';

export default combineReducers({
auth: AuthReducer
// employeeForm: EmployeeFormReducer,
// employees: EmployeeReducer
});

AuthReducer.js

  import {
EMAIL_CHANGED,
PASSWORD_CHANGED
} from '../actions/types';

const INITIAL_STATE = {
email: '',
password: ''
};

export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case EMAIL_CHANGED:
return { ...state, email: action.payload };
case PASSWORD_CHANGED:
return { ...state, password: action.payload };
default:
return state;
}
};

This is the error returned

最佳答案

我也面临这个问题并花了很多时间..这个问题在这个组件中不起作用react-redux connect。因为执行

export class Form extends Component

因此不执行

export default connect(
mapStateToProps,
emailChanged,
)(Form);

解决方案:

删除导出关键字例如:

 class Form extends Component

并导入该组件必须使用的位置

 import Form  from './src/components/Form';

不要使用

 import { Form } from './src/components/Form';

在这种情况下进行如下更改,

index.android.js

  import Form  from './src/components/Form';

Form.js

export class Form extends Component {
...
}
export default connect(
mapStateToProps,
emailChanged ,
)(Form);

希望这对您有帮助...快乐编码...!

关于reactjs - 连接 redux 功能不适用于 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934293/

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