关闭后如何清除模态中的反应状态?

Luk*_*sko 6 javascript forms reactjs

我有一张显示产品详细信息的产品卡。在底部,有一个 'edit' button。当clicked它显示带有预填充input字段的模式时,可以对其进行编辑然后保存。Modal 也可以在不保存的情况下关闭(但输入字段已编辑)

我的问题是,当用户编辑字段,然后关闭模式(不保存),然后再次打开时,字段未设置为初始值,但显示已更改。

我尝试了一个具有初始状态的变量,然后在关闭用它填充状态后,但它没有用。也试图对裁判做出反应,没有快乐。

import React, { Component } from 'react'
import Modal from 'react-modal';

const customStyles = {
...
};

Modal.setAppElement('#root');

class AdminButtons extends Component {

    state = {
        modalIsOpen: false,
    }

    componentDidMount() {
        const { id, inStock, name, price, type } = this.props.product
        this.setState({ id, inStock, name, price, type })
    }

    openModal = () => {
        this.setState({ modalIsOpen: true });
    }

    afterOpenModal = () => {
        ...
    }

    closeModal = () => {
        this.setState({ modalIsOpen: false });
    }

    handleChange = (event) => {
        const target = event.target
        const input = target.value
        const name = target.name
        this.setState({ [name]: input })
    }

    render() {
        const { product, remove } = this.props
        const { modalIsOpen, name, inStock, price, type } = this.state
        return (
            <>
                <button onClick={this.openModal}>EDIT</button>
                <Modal
                    isOpen={modalIsOpen}
                    onAfterOpen={this.afterOpenModal}
                    style={customStyles}
                    contentLabel="Edit "
                >
                    <h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
                    <button onClick={this.closeModal}>close</button>
                    <div>{this.props.product.name}</div>
                    <form>
                        <label>
                            Name
                            <input name="name" type="text" value={name} onChange={this.handleChange} />
                        </label>
                        <label>inStock
                            <input name="inStock" type="text" value={inStock} onChange={this.handleChange} />
                        </label>
                        <label>
                            Price
                            <input name="price" type="text" value={price} onChange={this.handleChange} />
                        </label>
                        <label>
                            Type
                            <input name="type" type="text" value={type} onChange={this.handleChange} />
                        </label>
                        <button onClick={ () => {
                            this.props.edit(this.state)
                            this.closeModal() }
                            }>Save changes</button>
                    </form>
                </Modal>
                {product.isRemoved ?
                    <button> add </button> :
                    <button onClick={() => remove(product.id)}>remove</button>
                }
            </>
        )
    }
}

Run Code Online (Sandbox Code Playgroud)

San*_*ndy 10

您的问题是模式没有被卸载,只是没有显示。可以使用以下方法修复此问题:

{modalIsOpen && <Modal
  isOpen={modalIsOpen}
  onAfterOpen={this.afterOpenModal}
  style={customStyles}
  contentLabel="Edit "
> modal content here! </Modal>
Run Code Online (Sandbox Code Playgroud)


Ric*_*lez 7

如果来自输入的数据在您的组件中,您可以尝试如下操作:closeModal您可以在其中设置组件的初始状态


const initialState = { name: null, inStock: null, price: null, type:null }

closeModal = () => {
        this.setState({ 
         ...initialState,
         modalIsOpen: false 
        });
    }
Run Code Online (Sandbox Code Playgroud)

但是,如果输入的状态来自父组件,则需要一种新方法来重置父组件的数据,该数据可以作为回调添加到同一方法中。

const initialState = { name: null, inStock: null, price: null, type:null }

closeModal = () => {
        this.setState({ 
         modalIsOpen: false 
        }, () => {
        this.props.resetInputData();
      });
    }
Run Code Online (Sandbox Code Playgroud)