简体   繁体   English

将 class 组件重构为功能性参考错误

[英]Refactoring a class component to Functional, ReferenceError

I am trying to refactor a class component, but in the class one, there is a state with map and I tried changing it to Functional and used useState but it keeps giving me this error I am trying to refactor a class component, but in the class one, there is a state with map and I tried changing it to Functional and used useState but it keeps giving me this error

 ReferenceError: Cannot access 'rules' before initialization

it happens when I'm trying to refactor the State of rules(which I'm not sure how), with map, to useState.当我尝试使用 map 将规则的 State 重构为 useState 时,就会发生这种情况。 Is it even the correct way of assigning state for map and how can I fix it?它甚至是为 map 分配 state 的正确方法,我该如何解决?

the class component: class 组件:

import Rule from "./Rule";

class Game extends Component {

state = {
    dices: Array(this.props.nOfDices).fill(1),
    locked: Array(this.props.nOfDices).fill(false),
    rotation: Array(this.props.nOfDices).fill(0),
    rollsRemaining: 3,
    isRolling: false,
    rules: this.props.rules.map( r => ({...r})),
    score: 0,
    bestScore: window.localStorage.getItem("bestScore") || "0"
};

componentDidMount() {
    this.roll();
};

my refactored functional component:我重构的功能组件:

const Game = ({ nOfDices }) => {
const [isRolling, setisRolling] = useState(false);
const [score, setScore] = useState(0);
const [rollsRemaining, setRollsRemaining] = useState(3);
const [dices, setDices] = useState([Array(nOfDices).fill(1)]);
const [rules, setRules] = useState(rules.map(r => ({ ...r })));
const [bestScore, setBestScore] = useState(window.localStorage.getItem("bestScore") || "0");
const [locked, setLocked] = useState([Array(nOfDices).fill(false)]);
const [rotation, setRotation] = useState([Array(nOfDices).fill(0)]);


useEffect(() => {
    roll();
    //eslint-disable-next-line
}, []);

You are currently setting rules to a map of itself...您当前正在为自己的 map 设置rules ...

const [rules, setRules] = useState(rules.map(r => ({ ...r })));

should it be coming from props as it is in the original?它应该像原版一样来自道具吗?

state = {
  // ...
  rules: this.props.rules.map( r => ({...r})),
  // ...
}

If so you'll need to also destructure it out of props in the parameter declaration.如果是这样,您还需要从参数声明中的道具中解构它。 (Here renaming it to avoid collision with the the state name Game = ({rules: _rules, nOfDices}) =>... ) (这里重命名它以避免与 state 名称Game = ({rules: _rules, nOfDices}) =>...

Something like...就像是...

const Game = ({ rules: _rules, nOfDices }) => {
  const [isRolling, setisRolling] = useState(false);
  const [score, setScore] = useState(0);
  const [rollsRemaining, setRollsRemaining] = useState(3);
  const [bestScore, setBestScore] = useState(window.localStorage.getItem('bestScore') || '0');
  // nOfDices
  const [dices, setDices] = useState([Array(nOfDices).fill(1)]);
  const [locked, setLocked] = useState([Array(nOfDices).fill(false)]);
  const [rotation, setRotation] = useState([Array(nOfDices).fill(0)]);
  // rules
  const [rules, setRules] = useState(_rules.map((r) => ({ ...r })));

  // update state if `nOfDices` changes in props
  useEffect(() => {
    setDices([Array(nOfDices).fill(1)]);
    setLocked([Array(nOfDices).fill(false)]);
    setRotation([Array(nOfDices).fill(0)]);
  }, [nOfDices]);

  // update state if `_rules` changes in props
  useEffect(() => {
    setRules(_rules.map((r) => ({ ...r })));
  }, [_rules]);

  useEffect(() => {
    roll();
    //eslint-disable-next-line
  }, []);

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

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