简体   繁体   English

如何以当前状态进行推送

[英]How to do a push with the current state

I am creating an application with tabs. 我正在创建带有标签的应用程序。 These tabs are created by clicking on a menu item. 这些选项卡是通过单击菜单项创建的。 My problem is that repeated tabs are created. 我的问题是创建了重复的选项卡。 If you have already clicked on a menu item, I need you to do nothing (do not add it to my array). 如果您已经单击了菜单项,则无需执行任何操作(请勿将其添加到我的数组中)。 console.log (newArrayTabs) works perfectly and does not add elements to my array, but I do not know how to use it in the push . console.log (newArrayTabs)可以正常工作,并且不会向数组中添加元素,但是我不知道如何在push使用它。 If in the push I try to use newArrayTabs it does not work. 如果在push我尝试使用newArrayTabs则它不起作用。

I edit my code with @FunkeyFlo and i changed some code. 我用@FunkeyFlo编辑代码,然后更改了一些代码。 This is the correct code for fix my issue . 这是解决我的问题的正确代码

class App extends Component {
    constructor(props, context){
        super(props, context);

        ["openTabs",].forEach((method) => {
            this[method] = this[method].bind(this);
        });

        this.state = {
            navigation: {
                menu: [],
            },
            tabs:{
                tabsLi:[],    
            },

            textvalue : "",
            showtabs: true,
        }
    }

    componentDidMount() {
        fetch('json_menuFIN.php')
            .then(response => response.json())
            .then(data =>{
                this.setState({navigation: data});
                //console.log(data)
            })
    }

    openTabs(e, url, iframe, trdtitle){
        e.preventDefault();
                const { tabs } = this.state;
        this.setState({
            showtabs: false,
        })
        if (this.state.tabs.tabsLi.includes(trdtitle) === false){
            this.setState({
                tabs: { tabsLi:[...new Set(this.state.tabs.tabsLi),trdtitle]},
            })
        }
    }
class Tabs extends Component {
    render(){
        return(
            <div id="content-tabs" className="tabs">
            {( this.props.showtabs)  
                ? (
                    <>
                    <div className="waiting-leads">
                        <p>Parece que todavía no hay ningún lead...</p>
                        <h3>¡Ánimo, ya llega!</h3>
                        <img src={imgDinosaurio} alt="Dinosaurio"></img>
                    </div>
                    </>
                ) : (
                    <ul id="resizable" className="content" >
                        <List list={this.props.tabs.tabsLi} />
                    </ul>
            )}
            </div>
        );
    }
}

const List = (props) => {
    return (
        <>
        {props.list.map((value, index) =>         
            <li key={index}>
                <span>{value}</span>
                <Icon icon="cerrar" className='ico-cerrar' /*onClick={remove_tab(index)}*//>
            </li>
        )}
        </>
    )
}

Your example is a bit confusing as tabs and tabsLi seem to be provided to the state as well as tabsLi being contained in tabs(?) 您的示例有点令人困惑,因为似乎向状态提供了tabs和tabsLi,并且tabsLi包含在tabs(?)中

But to update an array in your state you could do the following instead 但是要在您的状态下更新数组,您可以执行以下操作

  openTabs(e, url, iframe, trdtitle) {
    e.preventDefault();

    const { tabs } = this.state;
    this.setState({
      //...
      tabsLi: [...tabs.tabsLi, trdtitle], //instead of push
      //...
    })
  }

EDIT: it also works fine with components 编辑:它也与组件一起正常工作

import React, { Component } from 'react';
import { render } from 'react-dom';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'React',
      listObj: { list: ['first value'] }
    };
  }

  onClick = () => {
    const newValue = 'new value';

    const { listObj } = this.state;
    this.setState({
      //...
      listObj: { list: [...listObj.list, newValue] }, //instead of push
      //...
    })
  }

  render() {
    return (
      <div onClick={this.onClick}>
        <List list={this.state.listObj.list} />
      </div>
    );
  }
}

const List = (props) => {
  return <div>{props.list.map((i) => <div>{i}</div>)}</div>
}

render(<App />, document.getElementById('root'));

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

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