简体   繁体   English

如何在ReactJs中设置各个状态以在map函数内呈现组件

[英]How to set individual states to render components inside a map function in ReactJs

I have a react component which has 3 radio buttons.Along with the radio buttons, I have an edit button which opens up a separate component when that particular edit button inside the radio button is clicked.But, the last radio button will not have the edit button inside the radio button. 我有一个带有3个单选按钮的react组件。除了单选按钮外,我还有一个编辑按钮,当单击单选按钮内的特定编辑按钮时,它会打开一个单独的组件。但是,最后一个单选按钮不会有单选按钮内的编辑按钮。 So,to render the edit component on click of the edit button, I have added state value inside my component as: 因此,要在单击编辑按钮时呈现编辑组件,我在组件内添加了状态值:

this.state = {
      isDrawerOpen: false,
      options: ['abc.sce.com', 'def.sce.com', 'No email alerts'],
}

So, I am rendering my radio buttons using a map function and below the radio button the new component should be called on the click of the edit button which is inside the radio button. 所以,我使用地图功能渲染我的单选按钮,在单选按钮下面,单击单选按钮内的编辑按钮时,应调用新组件。

So, my code looks like below: 所以,我的代码如下所示:

{this.state.options.map((email, i, arr) => {
          return (
           <RadioButtonComponent />   
                {!(arr[i].includes('No email alerts')) ? (
                  <button
                      id='editEmail'
                      onClick={this.handleEmailToggle} /> ) :
                  null}

               {this.state.isDrawerOpen ?  <OpenComponentOnEditClickOfRadiButton /> : null}

          );

My handleEmailToggle() function which is called on the edit button click is as follows: 我在编辑按钮单击时调用的handleEmailToggle()函数如下:

  handleEmailToggle() {
    this.setState({ isDrawerOpen: true });
  }

So, when the edit button is clicked, the component <OpenComponentOnEditClickOfRadiButton /> should open.Now what is happening is when I am clicking on any one of the edit button of the radio button,the component <OpenComponentOnEditClickOfRadiButton /> is opening for all the radio buttons, even for the last radio button which doesn't have the edit button.So, how to set individual state for each edit button and render the new component only for that corresponding button? 因此,当单击编辑按钮时,组件<OpenComponentOnEditClickOfRadiButton />应该打开。现在发生的事情是当我点击单选按钮的任何一个编辑按钮时,组件<OpenComponentOnEditClickOfRadiButton />正在打开所有单选按钮,即使是最后一个没有编辑按钮的单选按钮。那么,如何为每个编辑按钮设置单独状态并仅为该对应按钮呈现新组件?

you should try this: 你应该试试这个:

{this.state.options.map((email, i, arr) => {
      return (
       <RadioButtonComponent />   
            {!(arr[i].includes('No email alerts')) ? (
              <button
                  id='editEmail'
                  onClick={e => this.handleEmailToggle(e, email)} /> ) :
              null}

           {this.state.isDrawerOpen ?  <OpenComponentOnEditClickOfRadiButton /> : null}

      );

and update your handleEmailToggle to: 并将handleEmailToggle更新为:

handleEmailToggle(e, email) {
  // use email passed from map
  this.setState({ isDrawerOpen: true });
}

Long ago, I faced the same issue as yours. 很久以前,我遇到了和你一样的问题。 I have done the following which did a trick: 我做了以下诀窍:

const EditComp = () => <button type='button'>Edit</button>

class App extends Component {
    state = {
      isDrawerOpen: [],
      radioButton : [
        { id: "radio1", name: "male", edit: false,value: "male" },
        { id: "radio2", name: "female", edit: false, value: "female" }
      ],
      val: []
    };


  handleEmailToggle(event, currentIndex) {
    let showEdit = this.state.isDrawerOpen.map((val, index) => {
        return (val = false);
    }); 
    let isDrawerOpen = showEdit.slice();
    isDrawerOpen[currentIndex] = true;
    this.setState({ isDrawerOpen });
  }

  render() {
    return (
      this.state.radioButton.map((ele,index) => (
        <>
            <button key={ele.id} 
              onClick={e => this.handleEmailToggle(e, index)}>
            {ele.name} </button><br/>
            {this.state.isDrawerOpen[index] ? <EditComp /> : null}
            <br/>

        </>
      ))
    );
  }
}

what I did is, on each button click showed a edit button respectively.On every click current element index will be passed to the function and there's a state isDrawerOpen which stores each index with true/false respectively on particular element. 我所做的是,在每个按钮上分别单击显示一个编辑按钮。每次单击当前元素索引将传递给该函数,并且有一个状态isDrawerOpen,它将每个索引分别存储在特定元素的true / false。

Hope this helps to solve your query. 希望这有助于解决您的查询。 Code demo 代码演示

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

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