繁体   English   中英

使用路由时 React 组件没有更新?

[英]React component isn't updated when using Routes?

这是我第一次使用 react-router 并且我很难做到。

这是我的代码:

应用程序.tsx

import React from 'react';
import logo from './logo.svg';
import { BrowserRouter as Router, Link, Route } from "react-router-dom";
import DraggableGame from './PointAndClickGame';
import PointAndClickGame from './PointAndClickGame';
import './css/App.css';

function App() {
  return (
    <div className="App">
      <Router>
        <Route exact path='/'>
         <Link to="/pointAndClick">joc pointAndClick</Link><br/>
         <Link to="/draggable">joc draggable</Link>
        </Route>
        <Route exact path="/pointAndClick">
          <PointAndClickGame/>
        </Route>
        <Route exact path="/draggable">
          <DraggableGame/>
        </Route>
      </Router>   
    </div>
  );
}

export default App;

DraggableGame.tsx

import React, { useRef, useState, useEffect } from 'react';
import { Row, Col } from 'react-bootstrap';
import Box from './Box';
import ButtonCustom from './Button';

function DraggableGame() {
    const correctResponse: string = "A";
    const [selectedResponse, setResponse] = useState<string|null>(null);

    const onSubmitAnswer = () => {
        class Coordinate {
            x: number;
            y: number;

            constructor(x: number, y: number) {
                this.x = x;
                this.y = y;
            }
        }

        let width: number = box0.current.getBoundingClientRect().width;
        let height: number = box0.current.getBoundingClientRect().height;

        let boxCoordinates: Array<Coordinate> = [];
        for(let i=0;i<4;i++)
        {
            let data = eval(`box${i+1}`);
            boxCoordinates.push(new Coordinate(data.current.getBoundingClientRect().x, data.current.getBoundingClientRect().y));
        }     
        
        if(Math.abs(boxCoordinates[0].x - box0.current.getBoundingClientRect().x) < (width / 1.75))
        {
            if(Math.abs(boxCoordinates[0].y - box0.current.getBoundingClientRect().y) < (height / 1.75))
            {
                setResponse("A");
            }            
        }
        else if(Math.abs(boxCoordinates[1].x - box0.current.getBoundingClientRect().x) < (width/1.75))
        {
            if(Math.abs(boxCoordinates[1].y - box0.current.getBoundingClientRect().y) < (height/1.75))
            {
                setResponse("B");
            }            
        }
        else if(Math.abs(boxCoordinates[2].x - box0.current.getBoundingClientRect().x) < (width /1.75))
        {
            if(Math.abs(boxCoordinates[2].y - box0.current.getBoundingClientRect().y) < (height /1.75))
            {                       
                setResponse("C");
            }            
        }
        else if(Math.abs(boxCoordinates[3].x - box0.current.getBoundingClientRect().x) < (width/1.75))
        {
            if(Math.abs(boxCoordinates[3].y - box0.current.getBoundingClientRect().y) < (height/1.75))
            {
                setResponse("D");
            }            
        }
    };

    const box0 = useRef<any>();
    const box1 = useRef<any>();
    const box2 = useRef<any>();
    const box3 = useRef<any>();
    const box4 = useRef<any>();   
    
    return (
        <div>
            <Box {...{ letter: correctResponse, draggable: 1 }} ref={box0} />
            <Row>
            <Col md>
            <Box {...{ letter: 'A', draggable: 0 }} ref={box1} />
            </Col>
            <Col md>
            <Box {...{ letter: 'B', draggable: 0 }} ref={box2} />
            </Col>
            <Col md>
            <Box {...{ letter: 'C', draggable: 0 }} ref={box3} />
            </Col>
            <Col md>
            <Box {...{ letter: 'D', draggable: 0 }} ref={box4} />
            </Col>
            </Row>
            <br/>
            { selectedResponse === correctResponse ? <ButtonCustom {...{onSubmitAnswer: onSubmitAnswer, resolvedQuestion: 1}}/> : <ButtonCustom {...{onSubmitAnswer: onSubmitAnswer, resolvedQuestion: 0}}/> }
        </div>
    );
}

export default DraggableGame;

PointAndClickGame.tsx

import React, { useState, SyntheticEvent } from 'react';
import { Row, Col } from 'react-bootstrap'
import Box from './Box';
import ButtonCustom from './Button';


function PointAndClickGame() {
    const correctResponse: string = "C";
    const [selectedResponse, setResponse] = useState<string|null>(null);

    const onSelectAnswer = (event: SyntheticEvent<HTMLDivElement, Event>) => {
        setResponse(event.currentTarget.textContent);           
    };

    return (
        <div>
            <Box letter={correctResponse} onSelectFunction={onSelectAnswer} />
            <Row>
            <Col md>
            <Box letter="A" onSelectFunction={onSelectAnswer} />
            </Col>
            <Col md>
            <Box letter="B" onSelectFunction={onSelectAnswer} />
            </Col>
            <Col md>
            <Box letter="C" onSelectFunction={onSelectAnswer} />
            </Col>
            <Col md>
            <Box letter="D" onSelectFunction={onSelectAnswer} />
            </Col>
            </Row>
            <br/>
            { selectedResponse === correctResponse ? <ButtonCustom {...{resolvedQuestion: 1}}/> : <ButtonCustom {...{resolvedQuestion: 0}}/> }
        </div>
    );
}

export default PointAndClickGame;

问题是无论我从 App 组件按下哪个链接,React 总是渲染 pointAndClick,我不明白为什么。

为什么会发生这种情况,我该如何解决?

简单地说,这是错误的部分

import DraggableGame from './PointAndClickGame';
import PointAndClickGame from './PointAndClickGame';

您正在导入相同的组件。 由于将组件导出为default您可以使用任何名称导入它,并且不会抛出任何错误警告您

暂无
暂无

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

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