2017-08-11 45 views
1

新手在這。Redux窗體 - 初始值不加載的形式

我有一個相當長的窗體,用作編輯窗體來更改客戶端細節。表單加載但不加載任何值。建議硬編碼字段的某些值,例如「foo」和「bar」,然後在文本字段中獲取這些字符串。

這是我在形式硬編碼的工作初始值的結束碼:

let ClientForm = reduxForm({ 
    form: CLIENT_FORM_NAME, 

    })(Client) 

     ClientForm = connect(
      state => ({ 
      initialValues: { account: 'foo', bsb: 'bar', } 
      }), 
      { reducer } // bind client loading action creator 
     )(ClientForm) 

    export default ClientForm 

但是,如果我用下面的代碼沒有任何反應,即使「state.editClient」有值。

 let ClientForm = reduxForm({ 
    form: CLIENT_FORM_NAME, 

    })(Client) 

     ClientForm = connect(
      state => ({ 
      initialValues: state.editClient // pull initial values from client reducer 
      }), 
      { reducer } // bind client loading action creator 
     )(ClientForm) 

    export default ClientForm 

現在我導入使用以下導入我的減速器:

 import reducer from '../edit/reducer' 

沒有錯誤 - 它發現這一點。

這裏是減速機:

import { fetch, addTask } from 'domain-task' 
    import { getJwt } from '../../../auth/jwt' 
    import { handleErrors } from '../../../utils/http' 
    import { 
    REQUEST_CLIENT_TO_EDIT, 
    RECEIVE_CLIENT_TO_EDIT, 
    ERROR_CLIENT_EDIT, 
    } from './actions' 

    const initialState = { 
    isLoading: false, 
    isEditMode: null, 
    error: null, 
    id: null, 
    clientNo: null, 
    company: false, 
    companyName: null, 
    abn: null, 
    isWarrantyCompany: false, 
    requiresPartsPayment: false, 
    companyEmail: null, 
    clientFirstName: null, 
    clientLastName: null, 
    mobilePhone: null, 
    phone: null, 
    email: null, 
    notes: null, 
    bankName: null, 
    bsb: null, 
    account: null, 
    activity: false, 
    active: false, 
    dateCreated: null, 
    userName: null, 
    } 

    export default (state = initialState, action) => { 
    switch (action.type) { 
     case REQUEST_CLIENT_TO_EDIT: 
     return { 
      ...state, 
      id: action.payload, 
      isLoading: true, 
      error: null, 
     } 

     case RECEIVE_CLIENT_TO_EDIT: 
     return { 
      ...state, 
      ...action.payload, 
      isLoading: false, 
      error: null, 
     } 

     case ERROR_CLIENT_EDIT: 
     return { 
      ...state, 
      isLoading: false, 
      error: action.payload, 
     } 

     default: 
     return state 
    } 
    } 

這是由國家的行動和成果更新調用。

這裏是表示鍍鉻了Redux工具editClient圖片...

enter image description here

爲什麼不會頁面加載 「editClient」?我錯過了什麼或誤解了什麼。

順便說一句,這裏是客戶端的完整形式(我想它最好有完整的上下文重形式):

 import React, { PropTypes } from 'react' 
    import { Field, reduxForm, FormSection } from 'redux-form' 
    import { connect } from 'react-redux' 
    import { Col, Row } from 'react-bootstrap' 
    import { Button, Glyphicon, Panel } from 'react-bootstrap' 
    import Moment from 'moment' 
    import Address from '../../address/addressContainer' 
    import FormField from '../../formComponents/formField' 
    import CheckboxField from '../../formComponents/checkboxField' 
    import TextField from '../../formComponents/textField' 
    import StaticText from '../../formComponents/staticText' 
    import TextAreaField from '../../formComponents/textAreaField' 
    import DateField from '../../formComponents/datefield' 

    import reducer from '../edit/reducer' 

    export const CLIENT_FORM_NAME = 'Client' 

    const required = value => (value ? undefined : 'Required') 
    const maxLength = max => value => 
    value && value.length > max ? `Must be ${max} characters or less` : undefined 
    const number = value => 
    value && isNaN(Number(value)) ? 'Must be a number' : undefined 
    const minValue = min => value => 
    value && value < min ? `Must be at least ${min}` : undefined 
    const email = value => 
    value && !/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) 
     ? 'Invalid email address' 
     : undefined 
    const tooOld = value => 
    value && value > 65 ? 'You might be too old for this' : undefined 
    const aol = value => 
    value && /[email protected]\.com/.test(value) 
     ? 'Really? You still use AOL for your email?' 
     : undefined 

    const normalizeMobilePhone = value => { 
    if (!value) { 
     return value 
    } 

    const onlyNums = value.replace(/[^\d]/g, '') 
    if (onlyNums.length <= 4) { 
     return onlyNums 
    } 
    if (onlyNums.length <= 8) { 
     return `${onlyNums.slice(0, 4)} ${onlyNums.slice(4)}` 
    } 
    return `${onlyNums.slice(0, 4)} ${onlyNums.slice(4, 7)} ${onlyNums.slice(7, 10)}` 
    } 

    export const Client = (props) => { 
    const { 
     handleSubmit, 
     companyValue, 
     isWarrantyCompanyValue, 
     isEditMode } = props 

    const { reset } = props 

    return (
     <Row> 
     <Col md={12}> 
      <h2><Glyphicon glyph="edit" /> {isEditMode ? 'Edit' : 'New'} Client</h2> 
      <hr /> 
      <form onSubmit={handleSubmit} className="form-horizontal"> 
      {isEditMode && (
       <Panel header={<h3>Client - Basic Details</h3>}> 
       <Row> 
        <Field component={StaticText} 
        name="clientNo" 
        id="clientNo" 
        label="Client No." 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
        /> 

        <Field component={StaticText} 
        name="dateCreated" 
        id="dateCreated" 
        label="Date Created." 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
        /> 

        <Field component={StaticText} 
        name="userName" 
        id="userName" 
        label="Created By." 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
        /> 
       </Row> 

       <Row> 
        <Field 
        component={props => { 
         return (
         <StaticText {...props}> 
          <p 
          className="form-control-static" 
          > 
          <Glyphicon glyph={props.input.value ? 'ok' : 'remove'} /> 
          {' '}{props.input.value ? 'Has jobs attached' : 'No jobs attached'} 
          </p> 
         </StaticText> 
        ) 
        }} 
        name="activity" 
        id="activity" 
        label="Activity" 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
        /> 

        <Field component={CheckboxField} 
        name="active" 
        id="active" 
        label="De-Activate" 
        checkboxLabel="De activate this client" 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
        /> 
       </Row> 
       </Panel> 
      )} 

      <Panel header={<h3>Client - CompanyDetails</h3>}> 

       <Row> 
       <Field component={CheckboxField} 
        id="company" 
        name="company" 
        label="Company?" 
        checkboxLabel="Client represents a company" 
        fieldCols={6} 
        labelCols={3} 
        controlCols={9} 
       /> 
       </Row> 
       {companyValue && (
       <div> 
        <Row> 
        <Field component={TextField} 
         name="companyName" 
         id="companyName" 
         type="text" 
         label="Company Name" 
         placeholder="Enter company name..." 
         fieldCols={6} 
         labelCols={3} 
         controlCols={9} 
        /> 

        <Field component={TextField} 
         name="abn" 
         id="abn" 
         type="text" 
         label="ABN." 
         fieldCols={6} 
         labelCols={3} 
         controlCols={5} 
        /> 
        </Row> 
        <Row> 
        <Field component={CheckboxField} 
         id="isWarrantyCompany" 
         name="isWarrantyCompany" 
         label="Warranty Company?" 
         checkboxLabel="Client represents a warranty company" 
         fieldCols={6} 
         labelCols={3} 
         controlCols={9} 
        /> 
        {isWarrantyCompanyValue && (
         <Field component={CheckboxField} 
         id="requiresPartsPayment" 
         name="requiresPartsPayment" 
         label="Requires Parts Payment?" 
         checkboxLabel="We pay for parts" 
         fieldCols={6} 
         labelCols={3} 
         controlCols={9} 
         /> 
        )} 
        </Row> 
        <Row> 
        <Field component={TextField} 
         name="companyEmail" 
         id="companyEmail" 
         type="email" 
         label="Spare Parts Email." 
         placeholder="Enter spare parts email..." 
         fieldCols={6} 
         labelCols={3} 
         controlCols={9} 
        /> 
        </Row> 
       </div> 
      )} 
      </Panel> 

      <Panel header={<h3>Client - {companyValue ? 'Company Contact' : 'Personal'} Details</h3>}> 

       <Row> 
       <Field component={TextField} 
        name="clientFirstName" 
        id="clientFirstName" 
        type="text" 
        label="First Name." 
        placeholder="Enter first name..." 
        fieldCols={6} 
        labelCols={3} 
        controlCols={9} 
        validate={[required]} 
       /> 

       <Field component={TextField} 
        name="clientLastName" 
        id="clientLastName" 
        type="text" 
        label="Last Name." 
        placeholder="Enter last name..." 
        fieldCols={6} 
        labelCols={3} 
        controlCols={9} 
       /> 
       </Row> 

       <Row> 
       <Field component={TextField} 
        name="mobilePhone" 
        id="mobilePhone" 
        type="text" 
        label="Mobile No." 
        placeholder="Enter mobile No..." 
        fieldCols={6} 
        labelCols={3} 
        controlCols={5} 
        normalize={normalizeMobilePhone} 
       /> 

       <Field component={TextField} 
        name="phone" 
        id="phone" 
        type="text" 
        label="Phone No." 
        placeholder="Enter phone No..." 
        fieldCols={6} 
        labelCols={3} 
        controlCols={5} 
       /> 
       </Row> 

       <Row> 
       <Field component={TextField} 
        name="email" 
        id="email" 
        type="email" 
        label="Email." 
        placeholder="Enter email address..." 
        fieldCols={6} 
        labelCols={3} 
        controlCols={9} 
       /> 
       </Row> 
      </Panel> 

      <FormSection name="Address"> 
       <Address /> 
      </FormSection> 

      <Panel header={<h3>Notes</h3>}> 
       <Row> 
       <Field component={TextAreaField} 
        id="notes" 
        name="notes" 
        label="Notes." 
        placeholder="Enter notes here..." 
        fieldCols={12} 
        labelCols={1} 
        controlCols={11} 
       /> 
       </Row> 
      </Panel> 

      <Panel header={<h3>Client - Bank Details</h3>}> 
       <Row> 
       <Field component={TextField} 
        name="bankName" 
        id="bankName" 
        type="text" 
        label="Bank Name." 
        placeholder="Enter bank name..." 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
       /> 

       <Field component={TextField} 
        name="bsb" 
        id="bsb" 
        type="text" 
        label="BSB No." 
        placeholder="Enter BSB No..." 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
       /> 


       <Field component={TextField} 
        name="account" 
        id="account" 
        type="text" 
        label="Account No." 
        placeholder="Enter Account No..." 
        fieldCols={4} 
        labelCols={4} 
        controlCols={8} 
       /> 
       </Row> 
      </Panel> 

      <div className="panel-body"> 
       <Row> 
       <Col xs={4}> 
        <Row> 
        <Col xs={8} xsOffset={4}> 
         <Button bsStyle="primary" type="submit" bsSize="small"> 
         <Glyphicon glyph="ok" /> Submit 
         </Button> 
         {' '} 
         <Button type="reset" bsSize="small" onClick={reset}> 
         <Glyphicon glyph="ban-circle" /> Clear 
         </Button> 
        </Col> 
        </Row> 
       </Col> 
       </Row> 
      </div> 
      </form> 
     </Col> 
     </Row > 
    ) 
    } 

    let ClientForm = reduxForm({ 
    form: CLIENT_FORM_NAME, 

    })(Client) 

     ClientForm = connect(
      state => ({ 
      initialValues: state.editClient // pull initial values from client reducer 
      }), 
      { reducer } // bind client loading action creator 
     )(ClientForm) 

    export default ClientForm 

回答

1

導出它之前,您正在重新定義ClientForm。

嘗試創建一個新變量來分配連接輸出。

let ClientForm = reduxForm({ 
    form: CLIENT_FORM_NAME, 

})(Client) 

let ClientForm2 = connect(state => ({ 
initialValues: { account: 'foo', bsb: 'bar', } 
     }), 
    { reducer } // bind client loading action creator 

)(ClientForm)

出口默認ClientForm2

+0

這實際上工作!你可以詳細說明我做錯了什麼,爲什麼你需要創建另一個變量來連接狀態.. – si2030

+0

在手機上,所以不能很好地解釋。你擁有它的方式,你將連接函數的輸出傳遞給自己 - 所以我們得到不可預知的行爲。通過創建一個單獨的變量,您可以將您的redux形式傳遞給connect函數的輸出。將clientform初始化爲一個const會更安全,這樣你就不會意外地再次遇到這個問題(或者至少會得到更好的錯誤信息) – hairmot