[Re-render rules]

This commit is contained in:
CPunisher 2020-05-03 23:38:22 +08:00
parent 7b812f3469
commit 693ac27ae6
4 changed files with 18 additions and 9 deletions

View File

@ -16,12 +16,16 @@ class QrItem extends React.Component {
};
}
componentDidMount() {
this.forceUpdate();
}
handleClick(e) {
this.props.onSelected(this.props.index);
}
shouldComponentUpdate(nextProps, nextState, nextContext) {
return nextProps.selected || this.props.selected
return (nextProps.selected || this.props.selected) && (this.props.text == nextProps.text || this.props.text.length == 0)
}
render() {

View File

@ -1,13 +1,7 @@
import React from "react";
import {srand, rand} from "../utils/util";
import './Qrcode.css'
function RandomNum(Min,Max){
const Random = Max - Min;
const random = Math.random();
const num = Min + random * Random;
return num;
}
function listPoint(props) {
if (!props.qrcode) return []
@ -20,7 +14,7 @@ function listPoint(props) {
for (let i = 0; i < nCount; i++) { //一维长度为5
sizeTable[i] = new Array(i); //在声明二维
for (let j = 0; j < nCount; j++) { //二维长度为5
sizeTable[i][j] = RandomNum(0.33,1);
sizeTable[i][j] = rand(0.33,0.8);
}
}
var nearPoint = new Array();

View File

@ -127,6 +127,7 @@ class Qrcode extends React.Component {
renderer={React.createElement(style.renderer, {
qrcode: this.state.qrcode,
})}
text={this.state.text}
selected={index == this.state.selectedIndex}
onSelected={this.handleSelected}
/>

10
src/utils/util.js Normal file
View File

@ -0,0 +1,10 @@
let seed = 0;
export function srand(sd) {
seed = sd;
}
export function rand(min, max) {
seed = (seed * 9301 + 49297) % 233280;
return min + (seed / 233280.0) * max;
}