简体   繁体   English

在className更改时ReactJS组件意外重新加载

[英]ReactJS component reloading unexpectedly on className change

TLDR; TLDR;
Does adding a className to an exciting div reload the parent component in ReactJs ? 将className添加到令人兴奋的div是否会在ReactJs中重新加载父组件? Added images near button to show console.log being called multiple times. 在按钮附近添加了图像,以显示被多次调用的console.log

Here is the bug.. 这是错误。

I'm building a simple 'order' app, which includes a sidebar. 我正在构建一个简单的“订单”应用程序,其中包括一个侧边栏。

I recreated the sidebar in a new project for a sanity check. 我在一个新项目中重新创建了侧边栏以进行健全性检查。 Same issues. 同样的问题。 Here is the simple version. 这是简单的版本。

class App extends Component {
  constructor() {
    super();
    this.state = {
      addList : [],
    }
  }
  render() {
    return (
      <div className="App">
        <Sidebar list = {this.state.addList}/>
      </div>
    );
  }
}

export default App;

and in the sidebar component 并在侧边栏组件中

class Sidebar extends Component {
    constructor(props){
        super(props);
        this.state = {
            active : false
        }
    }
    toggleSidebar () {
        if (this.state.active) {
            this.setState({
                active : false
            })
        } else {
            this.setState({
                active: true
            })
        }
    }
    render () {
        return (
            <div className={ 'sidebar ' + ((this.state.active) ? '' : 'hidden')}>
                <button className='tab' onClick={(e)=>{this.toggleSidebar()}}>
                        TAB
                </button>
                <div className="itemList">
                    {console.log(this.props.list)}
                </div>
            </div>
        )
    }
}

export default Sidebar;

The sideBar class has a position: fixed and I move it out of the screen on a button click and adding a hidden className ( .hidden { right: -x } ) sideBar类具有一个position: fixed ,单击按钮并添加hidden className( .hidden { right: -x } ),将其移出屏幕。

When an item gets selected in the parent app component, it gets added to its state ( addItem ). 在父级应用程序组件中选择某个项目时,该项目将添加到其状态( addItem )。

The Sidebar component has that property passed into so when addItem get a new item, it displays it. 补充工具栏组件具有该属性,因此,当addItem获取新项目时,它将显示它。 It works just as predicted. 它按预期运行。 I was able to add items and display them no problem. 我能够添加项目并显示它们没有问题。

I noticed the bug when I started adding number and doing price totals etc, because it seems the sidebar keep rendering I would find myself getting caught in infinite setState loops 我开始添加数字并进行价格总计等时注意到了该错误,因为似乎边栏不断呈现,我发现自己陷入了无限的setState循环中

Any solutions or advice? 有什么解决方案或建议吗?

Images for a those that are visual (clicking the tab and console displaying): 可视图像的图像(单击选项卡并显示控制台):

原始负载

点击后

多次点击后

The answer is simple, I'm foolish. 答案很简单,我很愚蠢。 In fact I AM changing the state of sidebar causes a reload. 实际上,我正在更改侧边栏的状态会导致重新加载。

Therefor to get around this is having the parent component hold the values and pass them down as properties for the sidebar to just display. 为此,要解决此问题,是使父组件保留这些值,并将其作为下边栏的属性向下传递以仅显示。 Therefor on reload the values don't change or re-add. 因此,在重新加载时,值不会更改或重新添加。

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

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