Styled Component无法在页面正常渲染,附相关代码求错误排查
问题核心原因
你定义的styled组件命名不符合React自定义组件规范:React 规定所有自定义组件必须首字母大写,小写开头的标签会被React识别为原生HTML标签,不会触发自定义组件的渲染逻辑,因此你写的testStyled组件无法正常渲染。
修复方案
只需要将styled组件的命名改为首字母大写,同时对应修改调用处的标签名即可,修复后代码如下:
import React from "react"; import styled from "styled-components"; // 首字母大写命名 const TestStyled = styled.h1` font-size:10px; `; export const Datepicker = () => { return ( <div> {/* 对应使用大写开头的标签调用 */} <TestStyled>Test</TestStyled> </div> ); };
额外排查项
如果修改命名后仍然无法正常显示,可执行npm list styled-components检查依赖是否正确安装,未安装的话执行npm install styled-components安装即可。
内容的提问来源于stack exchange,提问作者ahmedskulj10
相关产品推荐
相关产品推荐

