javascript - 如何防止 react 重新渲染整个组件?

标签 javascript reactjs ecmascript-6

我从 here 中改编了以下组件定义如下所示。但是,与示例不同的是,每次我在其上移动鼠标时,我的组件都会重新呈现。

重新渲染非常明显:

enter image description here

有人知道为什么会这样吗?

import React, { Component } from 'react';
import { PieChart, Pie, Sector } from 'recharts';
import { Segment, Header, Dimmer, Loader, Grid } from 'semantic-ui-react';

const renderActiveShape = (props) => {

const RADIAN = Math.PI / 180;
const { cx, cy, midAngle, innerRadius, outerRadius, startAngle, endAngle,
    fill, payload, percent, value } = props;
const sin = Math.sin(-RADIAN * midAngle);
const cos = Math.cos(-RADIAN * midAngle);
const sx = cx + (outerRadius + 10) * cos;
const sy = cy + (outerRadius + 10) * sin;
const mx = cx + (outerRadius + 30) * cos;
const my = cy + (outerRadius + 30) * sin;
const ex = mx + (cos >= 0 ? 1 : -1) * 22;
const ey = my;
const textAnchor = cos >= 0 ? 'start' : 'end';

return (
    <g>
        <text x={cx} y={cy} dy={8} textAnchor="middle" fill={fill}>{payload.name}</text>
        <Sector
            cx={cx}
            cy={cy}
            innerRadius={innerRadius}
            outerRadius={outerRadius}
            startAngle={startAngle}
            endAngle={endAngle}
            fill={fill}
            />
        <Sector
            cx={cx}
            cy={cy}
            startAngle={startAngle}
            endAngle={endAngle}
            innerRadius={outerRadius + 6}
            outerRadius={outerRadius + 10}
            fill={fill}
            />
        <path d={`M${sx},${sy}L${mx},${my}L${ex},${ey}`} stroke={fill} fill="none" />
        <circle cx={ex} cy={ey} r={2} fill={fill} stroke="none" />
        <text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} textAnchor={textAnchor} fill="#333">{`PV ${value}`}</text>
        <text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} dy={18} textAnchor={textAnchor} fill="#999">
            {`(Rate ${(percent * 100).toFixed(2)}%)`}
        </text>
    </g>
);
};

export default class TwoLevelPie extends Component {

constructor(props) {
    super(props);
    this.state = { activeIndex: 0 }
    this.onPieEnter = this.onPieEnter.bind(this);
}

onPieEnter(data, index) {
    this.setState({
        activeIndex: index
    });
}

render() {

    const data = [{ name: 'Group A', value: 400 }, { name: 'Group B', value: 300 },
    { name: 'Group C', value: 300 }, { name: 'Group D', value: 200 }];

    return (
        <Segment inverted>
            <PieChart width={800} height={400} onMouseEnter={this.onPieEnter}>
                <Pie
                    activeIndex={this.state.activeIndex}
                    activeShape={renderActiveShape}
                    data={data}
                    cx={300}
                    cy={200}
                    innerRadius={60}
                    outerRadius={80}
                    fill="#8884d8" />
            </PieChart>
        </Segment>
    );
}
}

最佳答案

定义为函数的纯组件将始终重新渲染。

将组件转换为类并防止重新渲染 shouldComponentUpdate()返回错误。

签名是shouldComponentUpdate(nextProps, nextState)。比如说,你通过验证组件的参数没有改变来防止重新渲染:

shouldComponentUpdate(nextProps, nextState){
   return !equals(nextProps, this.props); // equals() is your implementation
}

关于javascript - 如何防止 react 重新渲染整个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41763031/

相关文章:

javascript - 在 React JS 中构建引导模式和通知的正确方法?

javascript - 正则表达式如何用一个空格替换多个空格

javascript - ;(功能() {})();在 JavaScript 中

javascript - YouTube 订阅按钮未显示

javascript - 如何知道动态 "iterable"参数中的所有 Promise 何时已解决?

javascript - 如何使用嵌套结构记录 ES6 类成员?

javascript - 如何链接到页面并使用 Rails 激活特定选项卡

javascript - react : how to maintain performance when updating a large parent record from a controlled child input field?

javascript - react : Resizing a div including handlers, 不同的游标,即 ns-resize - 没有 jquery

javascript - 如何在 PreactJS 中包含外部 javascript 库?