简体   繁体   English

React 不会在函数中重新渲染组件

[英]React does not Rerender Components build in function

I do not understand the following behavior:我不明白以下行为:

I have written a component which should basically act as a menu.我编写了一个组件,它基本上应该充当菜单。 I am using MaterialUI for this.我为此使用 MaterialUI。 When a button in the menu is clicked, it should be set as "active" and visually represent this.单击菜单中的按钮时,应将其设置为“活动”并直观地表示这一点。 For this the "selected" value is set to true.为此,“selected”值设置为true。

If the button is clicked now, the state changes, but the list is not rendered again.如果现在单击按钮,状态会更改,但不会再次呈现列表。 What am I doing wrong?我究竟做错了什么? Or am I going a fundamentally wrong way to achieve my goal?或者我是否正在以一种根本错误的方式来实现我的目标?

import * as React from 'react';
import {List, ListItemButton, ListItemText } from '@mui/material';
import { ListItem } from '@mui/material';


class Sidemenu extends React.Component {
    menu = [];

    constructor(props) {
        super(props);
        this.state = {
            active: 'home'
        }

    }

    changeActiveItem = (element) => {
        this.setState({ active: element });
        this.render();
    }

    buildMenu(active) {
        console.log(active);
        var buildMenu = ['home', 'stammdaten'];

        for (let index = 0; index < buildMenu.length; index++) {
            const element = buildMenu[index];
            this.menu.push(
                <ListItem key={element}>
                    <ListItemButton divider selected={element == active ? true : false} onClick={() => {
                        this.changeActiveItem(element);
                        console.log(this.state);
                    }}>
                        <ListItemText>{element}</ListItemText>
                    </ListItemButton>
                </ListItem>
            )
        }   
    }

    render() {
        this.buildMenu(this.state.active);
        return (
            <List>

                {this.menu}


                {this.state.active}
                {/* {this.menu}               */}
            </List>
        )
    }
}

export default Sidemenu;
import * as React from 'react';
import { List, ListItemButton, ListItemText } from '@mui/material';
import { ListItem } from '@mui/material';


class Sidemenu extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            active: 'home'
        }

    }

    changeActiveItem = (element) => {
        this.setState({ active: element });
        this.render();
    }

    buildMenu() {
        var buildMenu = ['home', 'stammdaten'];
        return buildMenu.map((element) => {
            return (
                <ListItem key={element}>
                    <ListItemButton divider selected={element == this.state.active ? true : false} onClick={() => {
                        this.changeActiveItem(element);
                        console.log(this.state);
                    }}>
                        <ListItemText>{element}</ListItemText>
                    </ListItemButton>
                </ListItem>
            )
        })
    }

    render() {
        return (
            <List>
                {this.buildMenu()}
                {this.state.active}
            </List>
        )
    }
}

export default Sidemenu;

I now understand what the problem is.我现在明白是什么问题了。 React does update partially. React 会部分更新。 In this example I pass a variable, respectively an object to the specific place in the code.在这个例子中,我将一个变量或一个对象分别传递到代码中的特定位置。

React now compares the state before and after the state change. React 现在比较状态改变前后的状态。 Both before and after the update the same object is passed.在更新之前和之后传递相同的对象。

React does not detect any change and does not rerender the object. React 不会检测到任何更改,也不会重新渲染对象。

The solution is to parse the object into a new one.解决方案是将对象解析为一个新对象。

 {this.menu}

have to be changed to:必须改为:

 [...{this.menu}]

so with every state change a new object is recognized.因此,随着每次状态更改,都会识别出一个新对象。 However, the whole thing is to be enjoyed with some caution, in my special case, it is to be assumed that with each state change also the place must be rendered again.然而,整个过程需要谨慎,在我的特殊情况下,假设随着每个状态的变化,这个地方也必须再次渲染。

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

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