在生产版本中更改路线时,不会渲染内部组件

如何解决在生产版本中更改路线时,不会渲染内部组件

我的行为很奇怪,问题是当我为生产而构建时,这是路由逻辑App.tsx:

function App() {
  return (
    <main>
      <Route path="/" component={Agents} exact />
      <Route path="/:agentName" component={Agent} />
    </main>
  );
}

Agents 组件也很好, Agent 也没问题,但是我在 Agent 中有一个组件, AgentProfile 无法呈现的组件,我不知道为什么, Agent.tsx

import React,{ useEffect,useState } from 'react';
import { RouteChildrenProps,useParams } from 'react-router';
import { PuffLoader } from 'react-spinners';
import { Container,Row,Col } from 'react-bootstrap';

import classes from './Agent.module.scss';
import Navigation from '../Navigation/Navigation';
import AgentProfile from '../../components/AgentProfile/AgentProfile';
import Abilities from '../Abilities/Abilities';
import { IAgent,getAgent } from '../../API';

interface AgentProps extends React.Props<any>,RouteChildrenProps {}

export default function Agent(props: AgentProps) {
    const params: any = useParams();

    const [agentData,setAgentData] = useState<IAgent>();
    const [isAgentLoaded,setIsAgentLoaded] = useState(false);
    const [activeAbility,setActiveAbility] = useState(0);
    const [isLoading,setIsLoading] = useState(false);

    // fires when url changes
    useEffect(() => {
        // do the folowing only if 'agentName' param exists(/:agentName)
        if (params.agentName) {
            // gets the id from the queryParams
            const id: any = new URLSearchParams(props.location.search).get('id');

            setActiveAbility(0);
            setIsAgentLoaded(false);
            getAgent(id).then(agentData => {
                setIsAgentLoaded(true);
                setTimeout(() => {
                    setAgentData(agentData);
                },250);
            }).catch(error => console.log(error));
        }

    },[props.location,params]);
    
    return (
        <Container fluid style={{padding: 0}}>
            <Row className={classes.HeaderRow} noGutters>
                <Col xl={{offset: 1,span: 3}} className={classes.NavColumn}>
                    <Navigation />
                </Col>
                <Col xl="8">
                    {agentData && <AgentProfile // this doesn't get rendered at all
                        in={isAgentLoaded}
                        imgURL={agentData.imgURL}
                        role={agentData.role}
                        biography={agentData.biography} />}
                </Col>
            </Row>
            {agentData && (<Row className={classes.ContentRow} noGutters>
                <Col xl="6" style={{padding: 60}}>
                    <Abilities
                        abilities={agentData.abilities}
                        onClick={(index) => setActiveAbility(index)} />
                </Col>
                <Col xl="6">
                    <PuffLoader
                        size={200}
                        loading={isLoading}
                        css="margin: auto;"
                        color="white" />
                    <div
                        className={classes.Video}
                        style={{display: isLoading ? 'none' : 'flex'}}>
                        <video
                            loop
                            controls
                            autoPlay
                            onLoadStart={() => setIsLoading(true)}
                            onLoadedData={() => setIsLoading(false)}
                            src={agentData.abilities[activeAbility].videoURL}>
                        </video>
                    </div>
                </Col>
            </Row>)}
        </Container>
    )
}

我真的不知道问题出在什么地方,在开发模式下一切正常,问题出在生产版本中,没有找到解决方案。

一些屏幕截图,请注意chrome开发工具中的elements选项卡,我标记了确切的行: 开发中:

Development

生产中的

enter image description here

import React from 'react';
import { Transition } from 'react-transition-group';

import classes from './AgentProfile.module.scss';

const duration = 300;

const defaultStyle = {
    transition: `${duration}ms`,opacity: 0
};

// "in&hide" states animation styles
const agentInfoStyles: any = {
    entering: { opacity: 0,transform: 'translateY(-100px)' },entered:  { opacity: 1,transform: 'none' },exiting:  { opacity: 0,transform: 'translateY(100px)' },exited:   { opacity: 0,transform: 'none' }
};

// "in&hide" states animation styles
const agentImageStyles: any = {
    entering: { opacity: 0,transform: 'scale(0.9)' },transform: 'none' }
};

interface AgentProfileProps extends React.Props<any> {
    imgURL: string;
    role: string;
    biography: string;
    // Animation state control,true = animate "in to the VIEW",false = "hide from the VIEW"
    in: boolean;
}

export default function AgentProfile(props: AgentProfileProps) {
    return (
        <div className={classes.AgentContainer}>
            <Transition
                in={props.in}
                timeout={duration}>
                {state => (<>
                    <img
                        style={{
                            ...defaultStyle,...agentImageStyles[state]
                        }}
                        draggable={false}
                        src={props.imgURL}
                        alt="Agent"
                        className={classes.AgentImage} />

                    <div
                        className={classes.AgentInfo}
                        style={{
                            ...defaultStyle,...agentInfoStyles[state]
                        }}>
                        <span>//</span>
                        <p>{props.role}</p>
                        <span>//</span>
                        <p>{props.biography}</p>
                    </div>
                </>)}
            </Transition>
        </div>
    )
}

^^ AgentProfile ^^

解决方法

好吧,我仍然不知道为什么它不起作用,我是说应该,但是我设法通过更改路由配置使其起作用,例如: App.tsx

<main>
  <Route path="/" component={Agents} />
</main>

然后在 Agent.tsx

<Route path={`${props.match.url}:agentName`} render={() => (
  <AgentProfile
    in={isAgentLoaded}
    imgURL={agentData?.imgURL}
    role={agentData?.role}
    biography={agentData?biography} />
)}

而且,老实说,这样做是可行的,但是像这样配置路由确实有意义,但是,另一个路由没有错。它确实可以在开发模式下工作,但在生产模式下却不行。从来没有面对过这样的东西

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-