【问题标题】:React/Redux pre populating a form on a clickReact/Redux 在点击时预填充表单
【发布时间】:2016-04-16 03:05:53
【问题描述】:

单击Item 中的按钮后,我正在尝试预填充我的表单我有能够更新项目的操作,问题是能够使用正确的项目预填充表单能够调度该动作。

-----------------
| + List        | << Name of the list
-----------------
| SKU: 12345    | << Item
| ITEM: Bananas |
| PRICE: $1.00  |
|               |
| <edit>        | << Button triggering the modal and form. Need to pre-populate the form onClick
-----------------

/components/List.jsx

export class List extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      isModalOpen: false
    }
  }

  toggleModal () {
    this.setState({ isModalOpen: !this.state.isModalOpen })
  }

 ...

  render () {
    const { list, ...props } = this.props
    const listId = list.id

    return (
      ...

        <Items 
          items={props.listItems}
          openModal={this.toggleModal.bind(this)}>
        </Items>

        <Modal 
          className='list-add-item'
          openModal={this.state.isModalOpen}>
          <ItemForm 
            itemActions={this.props.itemActions} 
            listActions={this.props.listActions} 
            listId={listId}>
          </ItemForm>
        </Modal>
      </div>
    )
  }
}

/components/ItemForm.jsx

import React from 'react'
import uuid from 'node-uuid'

class ItemForm extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      isEditing: false
    }
  }

  handleSubmit = (event) => {
    event.preventDefault()

    let sku = this.refs.sku.value.trim()
    let text = this.refs.text.value.trim()
    let price = this.refs.price.value.trim()

    const item = { id: uuid.v4(), sku, text, price }

    // don't do anything if any field is left blank
    if (!item.sku || !item.text || !item.price) { return }

    this.props.itemActions.createItem(item)
    this.props.listActions.connectToList(this.props.listId, item.id)
    // reset the form after submission
    this.refs.itemForm.reset()
  }

  render() {
    const { ...props } = this.props

    return (
      <form ref="itemForm" onSubmit={this.handleSubmit}>
          <label>SKU</label>
          <input 
            type="text" 
            placeholder="SKU" 
            autoFocus={true}
            ref='sku'
            defaultValue={this.props.sku}
            onBlur={this.finishEdit}
            onKeyPress={this.checkEnter}></input>

          <label>Item</label>
          <input 
            type="text"
            placeholder="Item" 
            autoFocus={true}
            ref='text'
            defaultValue={this.props.text}
            onBlur={this.finishEdit}
            onKeyPress={this.checkEnter}></input>

          <label>Price</label>
          <input 
            type="text"
            placeholder="Price"
            autoFocus={true}
            ref='price'
            defaultValue={this.props.price}
            onBlur={this.finishEdit}
            onKeyPress={this.checkEnter}></input>

        <button type="submit">{ this.state.isEditing ? 'Edit Item' : 'Add Item' }</button>
      </form>
    )
  }
}

export default ItemForm

/components/Items.jsx

export default class Items extends React.Component {
  render () {
    const {items, onEdit, ...props} = this.props

    return (
      <ul className='items'>{items.map((item) =>
        <Item
          className='item'
          key={item.id}
          id={item.id}
          sku={item.sku}
          text={item.text}
          price={item.price}
          openModal={this.props.openModal}>
        </Item>
      )}</ul>
    )
  }
}

/components/Item.jsx

export default class Item extends React.Component {
  render () {
    const { openModal, ...props } = this.props

    return (
      <span>
        <li>SKU: {this.props.sku}</li>  
        <li>ITEM: {this.props.text}</li>
        <li>PRICE: {this.props.price}</li>
        <button onClick={this.props.openModal}>Edit Item</button>
      </span>
    )
  }
}

【问题讨论】:

    标签: forms reactjs rendering redux updating


    【解决方案1】:

    有不同的方法可以做到这一点。例如,您可以按照以下步骤操作:

    1. 组件/列表:添加一个内部状态以随时了解列表中的哪个项目被选中。将此selectedItem 作为道具传递给itemForm。创建一个将更新该状态的方法selectItem。将此 selectItem 方法作为道具传递给您的组件/项目。记得给组件bind它。
    2. 组件/项目:添加为onClick 处理程序,selectItem 作为道具传递给组件。
    3. components/itemForm:在constructor用props中传入的item信息创建一个状态。添加willReceiveProps 并在每次收到一些更新的道具(可能包含新的选定项目)时更新组件的内部状态。

    我希望这对您有所帮助并解决了您的问题。

    【讨论】:

    • 我不确定如何完成第一部分,我有一个 listItems 数组,其中包含属于 List 的所有 items,您将如何选择正确的项目那个数组?如果它让你更容易帮助我们可以在 Screenhero @LlorençMuntaner 上配对
    • 您可以将一个函数从List 传递给listItems,以便调用一个项目的onClick。该函数将负责更新父节点中的状态。
    【解决方案2】:

    我最终改用了 redux-form,我的应用程序的设置方式让我很难解决我遇到的问题。工作版本是here

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 1970-01-01
      • 2017-01-23
      • 2022-10-08
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 2012-07-20
      • 2020-10-24
      相关资源
      最近更新 更多