简体   繁体   English

在ReactJs中将道具传递给模态

[英]Passing props to modal in ReactJs

I work actually on my first react app. 我实际上是在第一个React应用上工作。 It's a collection app. 这是一个收集应用程序。 On this app, there is an admin dashbord, with all items of the app, and admin user can delete or modify item (like a traditional e-commerce back office). 在此应用程序上,有一个管理员仪表板,其中包含该应用程序的所有项目,并且管理员用户可以删除或修改项目(例如传统的电子商务后台)。 For modify item, admin user click on button "Modify Item", and a modal open for modify item information. 对于修改项目,管理员用户单击“修改项目”按钮,然后打开一个用于修改项目信息的模式。

My problem is when user open modal, the modal work fine, but i can't passing in props the information of the selected item (like brandname, reference, price ...). 我的问题是,当用户打开模态时,模态工作正常,但是我无法传递道具所选择项目的信息(例如品牌名称,参考,价格...)。 I'm very sorry to ask again a question in the same day, but i try many thing and no result. 很抱歉在同一天再次提出问题,但我尝试了很多事情,但没有结果。 A little help is very welcome. 一点帮助是非常欢迎的。

Thank you in advance, 先感谢您,

This is my code: 这是我的代码:

import React, { Component } from 'react';
import { database } from '../firebase/firebase';
import * as firebase from 'firebase';
import withAuthorization from './withAuthorization';
import _ from 'lodash';
import AuthUserContext from './AuthUserContext';
import FileUploader from "react-firebase-file-uploader";
import Image from 'react-image-resizer';
import{InstantSearch, SearchBox, Hits, Highlight, RefinementList} from "react-instantsearch/dom";
import Modal from 'react-responsive-modal';



function removeToCatalogue(hit) { 
  const item = hit.objectID;
    firebase.database().ref(`catalogue/${item}`).remove();
    console.log("Capsule correctement supprimée du catalogue")
}




const Hit = ({hit, onEdit}) =>
    <div className="item">
       <img src={hit.avatarURL} width={150} height={150}></img>
        <h1 className="marque">{hit.marque}</h1>
        <h3 className="numero">{hit.numero}</h3>
        <h4 className="reference">{hit.reference}</h4>
        <h4 className="marquesuite">{hit.marquesuite}</h4>
        <p className="cote">{hit.cote}</p>
        <button className="btn btn-warning" onClick={onEdit}>Modify Item</button>
        <button className="btn btn-danger" onClick={() => removeToCatalogue(hit)}>Supprimer</button> 
    </div>

  const Content = ({ onEdit }) => {

    const EnhancedHit = props =>
       <Hit onEdit={ onEdit } { ...props } />

    return (
      <div className="text-center">  
        <Hits hitComponent={ EnhancedHit } />
      </div>
    )
  }




class Admin extends React.Component {
  constructor (props) {
    super (props);

    this.state = {
      marque: '',
      marquesuite: '',
      numero: '',
      reference: '',
      cote: '',
      avatar: "",
      isUploading: false,
      progress: 0,
      avatarURL: "",
      catalogue: {},
      showModal: false,

    };
    this.onInputChange = this.onInputChange.bind(this);
    this.onHandleSubmit = this.onHandleSubmit.bind(this);

  }


  onOpenModal = () => {
    this.setState({ open: true });
  };

  onCloseModal = () => {
    this.setState({ open: false });
  };


  onInputChange(e) {
    this.setState({
      [e.target.name]: e.target.value
    });
  }

  onHandleSubmit(e){
    e.preventDefault();
    const catalogue = {
      marque: this.state.marque,
      marquesuite: this.state.marquesuite,
      numero: this.state.numero,
      reference: this.state.reference,
      cote: this.state.cote,
      avatar: this.state.avatar,
      avatarURL: this.state.avatarURL,

    };
    database.push(catalogue);
    this.setState({
      marque: '',
      marquesuite: '',
      numero: '',
      reference: '',
      cote: '',
      avatar: "",
      isUploading: false,
      progress: 0,
      avatarURL: "",
    });
  }

  handleUploadStart = () => this.setState({ isUploading: true, progress: 0 });
  handleProgress = progress => this.setState({ progress });
  handleUploadError = error => {
    this.setState({ isUploading: false });
    console.error(error);
  };

  handleUploadSuccess = filename => {
    this.setState({ avatar: filename, progress: 100, isUploading: false });
    firebase
      .storage()
      .ref("images")
      .child(filename)
      .getDownloadURL()
      .then(url => this.setState({ avatarURL: url }));
  };


  render (){
    const { open } = this.state;
    return (
      <div className="container-fluid">
        <div className="container">
        <h1 class="text-center">Espace d'Administration</h1>
        <a href="https://super-capsule.000webhostapp.com/signaler-modification" class="btn btn-primary btn-lg active" role="button" aria-disabled="true">Signaler une modification</a>
        <form onSubmit={this.onHandleSubmit}>
          <div className="form-group">
          <label>Marque de la capsule:</label>
          <input
            value={this.state.marque}
            type="text"
            name='marque'
            placeholder="Marque"
            onChange={this.onInputChange}
            ref="marque"
            className="form-control" />
          </div>
          <div>
          <label>Numéro de la capsule:</label>
          <input
            value={this.state.numero}
            type="text"
            name='numero'
            placeholder="Numéro de la capsule"
            onChange={this.onInputChange}
            ref="numero"
            className="form-control"/>
          </div>
          <div className="form-group">
          <label>Référence de la capsule:</label>
          <input
            value={this.state.marquesuite}
            type="text"
            name='marquesuite'
            placeholder="Référence de la capsule"
            onChange={this.onInputChange}
            ref="marquesuite"
            className="form-control"/>
          </div>
          <div className="form-group">
          <label>Référence de la capsule (suite):</label>
          <input
            value={this.state.reference}
            type="text"
            name='reference'
            placeholder="Référence de la capsule (suite)"
            onChange={this.onInputChange}
            ref="reference"
            className="form-control"/>
          </div>
          <div className="form-group">
          <label>Cote de la capsule:</label>
          <input
            value={this.state.cote}
            type="text"
            name='cote'
            placeholder="Cote de la capsule"
            onChange={this.onInputChange}
            ref="cote"
            className="form-control"/>
          </div>

          <label>Visuel de la capsule:</label>
          {this.state.isUploading && <p>Progress: {this.state.progress}</p>}
          {this.state.avatarURL && <img src={this.state.avatarURL} />}
          <FileUploader
            accept="image/*"
            name="avatar"
            randomizeFilename
            storageRef={firebase.storage().ref("images")}
            onUploadStart={this.handleUploadStart}
            onUploadError={this.handleUploadError}
            onUploadSuccess={this.handleUploadSuccess}
            onProgress={this.handleProgress}
          />
          <button className="btn btn-info">Ajouter une capsule</button>
        </form>
      </div>

        <h1 className="text-center">Catalogue de capsule</h1>



        <InstantSearch
            apiKey="xxx"
            appId="xxx"
            indexName="xxx">


            <SearchBox translations={{placeholder:'Rechercher une capsule'}} width="500 px"/>


            <Content onEdit={this.onOpenModal}/>  

              <div>
                <Modal open={open} onClose={this.onCloseModal} center>
                  <h2>Modification de la capsule</h2>
                  <p>Marque de la capsule:<input type="text" class="form-control" id="maj-marque" value=""></input></p>
                  <p>Numéro de la capsule:<input type="text" class="form-control" id="maj-num" value=""></input></p>
                  <p>Référence de la capsule:<input type="text" class="form-control" id="maj-ref" value=""></input></p>
                  <p>Référence de la capsule (suite):<input type="text" class="form-control" id="maj-refsuite" value=""></input></p>
                  <p>Cote de la capsule:<input type="text" class="form-control" id="maj-cote" value=""></input></p>
                  <button className="btn btn-success">Update</button>

                </Modal>
            </div>  


          </InstantSearch>



      </div>
    )
  }
}



const authCondition = (authUser) => !!authUser;

export default withAuthorization(authCondition)(Admin);

You don't need to pass props to the Modal component. 您无需将道具传递给Modal组件。 Instead, save whatever you want to display in the Admin state, and render it through there. 而是,保存要在Admin状态下显示的所有内容,然后通过该状态进行呈现。

In the Hit component, onEdit function should take an argument which is an object with all the information you need in the modal. Hit组件中, onEdit函数应采用一个参数,该参数是一个对象,其中包含模态中所需的所有信息。 Where you had <button className="btn btn-warning" onClick={onEdit}>Modify Item</button> will become: <button className="btn btn-warning" onClick={onEdit}>Modify Item</button>将变为:

    handleClick = () => {
        // I simplified the data you need to be only marque and numero here and below.
        onEdit({marque: hit.marque, numero: hit.numero})
    }
    <button className="btn btn-warning" onClick={handleClick}>Modify Item</button>

Then in the Admin component: 然后在Admin组件中:

    onOpenModal = (itemData) => {
        this.setState({ open: true, itemData  });
    };


    <Modal open={open} onClose={this.onCloseModal} center>
          <h2>Modification de la capsule</h2>
          <p>Marque de la capsule:<input type="text" class="form-control" id="maj-marque" value={this.state.itemData.marque}></input></p>
          <p>Numéro de la capsule:<input type="text" class="form-control" id="maj-num" value={this.state.itemData.numero}></input></p>
          <button className="btn btn-success">Update</button>
    </Modal>

Hope this helps. 希望这可以帮助。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM