【问题标题】:Redux Form Simple FieldsRedux 表单简单字段
【发布时间】:2016-11-11 06:47:30
【问题描述】:

我有一个简单的 Redux 表单并尝试按照此处给出的示例进行操作 https://redux-form.com/6.2.0/docs/GettingStarted.md/ 这是我的代码

user-form.js

import React from 'react';
import {Field, reduxForm} from 'redux-form';

class UserForm extends React.Component {

    /**
     * User Form goes here...
     */
    render() {
        const { handleSubmit } = this.props;

        return (
            <form role="form" onSubmit={handleSubmit}>
                <div className="box-body">
                    <div className="form-group">
                        <label htmlFor="name">Full Name</label>
                        <Field
                            name="name"
                            component="input"
                            type="text"
                            className="form-control"
                            placeholder="Enter full name..."/>
                    </div>
                    <div className="form-group">
                        <label htmlFor="email">Email address</label>
                        <Field
                            name="email"
                            type="email"
                            component="input"
                            className="form-control"
                            placeholder="Enter email"/>
                    </div>
                    <div className="form-group">
                        <label htmlFor="password">Password</label>
                        <Field
                            name="password"
                            type="password"
                            component="input"
                            className="form-control"
                            placeholder="Password"/>
                    </div>

                </div>
                <div className="box-footer">
                    {/* <button type="submit" className="btn btn-primary">Save</button> */}
                    <button type="submit" className="btn btn-primary" value="Save">Save</button>
                </div>
            </form>
        );
    }
}

UserForm = reduxForm({
    form: 'user'
})(UserForm);

export default UserForm;

上面的表单是由一个 UserPage Container 渲染的 user-page.js

import React from 'react';

import Page from '../../page';
import UserForm from '../components/user-form';
import UserList from '../components/user-list';

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import UserAction from '../actions';

import {showLoading, hideLoading} from 'react-redux-loading-bar';


/**
 * We might want to create an Abstract Form component to
 * include all common form features.
 */

class UserPage extends Page {




    handleUserSubmit(values) {
        console.log(values);
    }

    /**
     * Content is loaded into page
     */
    content() {
        return (
            <div className="row">
                {/* left column */}
                <div className="col-md-6">
                    {/* general form elements */}
                    <div className="box box-primary">
                        <div className="box-header with-border">
                            <h3 className="box-title">New User</h3>
                        </div>
                        {/* /.box-header */}
                        <UserForm onSubmit={this.handleUserSubmit}/>
                    </div>
                    {/* /.box */}

                </div>
                {/*/.col (left) */}
                {/* right column */}
                <div className="col-md-6">
                    {/* UserList made of <User /> */}

                    {this.userList()}
                    {/* /.box */}
                </div>
                {/*/.col (right) */}
            </div>
        );
    }


}

const mapStateToProps = (state) => ({ //this gets state from reducer and maps to props

            users: state.userList.users,
            fetched: state.userList.fetched,
            error: state.userList.error

});


const mapDispatchToProps = (dispatch) => ({
   actions: bindActionCreators({
       dispatchShowLoading: showLoading,
       dispatchHideLoading: hideLoading,
       dispatchUserList: UserAction.userList
   }, dispatch)
});


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

我的表单成功呈现,我可以在 Redux Dev 工具窗口中看到所有正在调度的操作,但是当我尝试在字段中输入文本时它不会做任何事情,但是这些操作就像我说的那样被调度。

抱歉,这听起来是一个非常基本的问题。我对 React 和 Redux 以及 Javascript 都比较陌生。

【问题讨论】:

    标签: javascript redux react-redux redux-form


    【解决方案1】:

    为了使 redux 表单工作,需要包含它的 reducer,我忘了包含一个,这解决了我的问题。

    import { reducer as formReducer } from 'redux-form';
    
        const allReducers = combineReducers({
            form: formReducer,
    
        });
    
        export default allReducers;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      相关资源
      最近更新 更多