React中创建style对象为组件添加样式时报错问题排查
React内联样式绑定报错问题排查
代码共存在4处直接导致报错、样式失效或编译失败的问题:
- 样式对象定义语法错误:声明
myStyle时缺少赋值操作符,原写法let myStyle{...}不符合JavaScript语法,会直接抛出语法错误,导致myStyle变量无法被正确初始化,正确写法为let myStyle = {...} - 样式值拼写错误:背景色属性值写为
balck属于拼写错误,正确颜色值为black,该错误会导致背景色样式无法正常渲染 - 样式属性名拼写错误:手风琴组件内有4处元素将内联样式属性
style错写为stle(缺失字母y),React无法识别该自定义属性,对应位置的样式不会生效 - JSX结构闭合错误:手风琴外层容器末尾多写了一个无匹配开始标签的
</div>,会导致JSX编译失败,直接阻断组件渲染
修正后可运行代码
import React from 'react' export default function About() { let myStyle = { color:'white', backgroundColor:'black' } return ( <> <h1 className='my-3'>about me</h1> <div className="accordion accordion-flush" id="accordionFlushExample"> <div className="accordion-item"> <h2 className="accordion-header" id="flush-headingOne"> <button style={myStyle} className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne"> Accordion Item #1 </button> </h2> <div style={myStyle} id="flush-collapseOne" className="accordion-collapse collapse" aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample"> <div className="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the first item's accordion body.</div> </div> </div> <div className="accordion-item"> <h2 className="accordion-header" id="flush-headingTwo"> <button style={myStyle} className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo"> Accordion Item #2 </button> </h2> <div style={myStyle} id="flush-collapseTwo" className="accordion-collapse collapse" aria-labelledby="flush-headingTwo" data-bs-parent="#accordionFlushExample"> <div className="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the second item's accordion body. Let's imagine this being filled with some actual content.</div> </div> </div> <div className="accordion-item"> <h2 className="accordion-header" id="flush-headingThree"> <button style={myStyle} className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree"> Accordion Item #3 </button> </h2> <div style={myStyle} id="flush-collapseThree" className="accordion-collapse collapse" aria-labelledby="flush-headingThree" data-bs-parent="#accordionFlushExample"> <div className="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the third item's accordion body. Nothing more exciting happening here in terms of content, but just filling up the space to make it look, at least at first glance, a bit more representative of how this would look in a real-world application.</div> </div> </div> </div> <div className='container' style={myStyle}> <button type="button" className="btn btn-primary my-3" >Primary</button> </div> </> ) }
注:修正代码已按需求为container容器绑定了
myStyle内联样式。
内容的提问来源于stack exchange,提问作者Narayan Dhungel
相关产品推荐
相关产品推荐

