gpt4 book ai didi

javascript - 将 html bootstrap 转换为 react.js jsx 样式后更改 css 样式

转载 作者:行者123 更新时间:2023-11-28 02:26:34 25 4
gpt4 key购买 nike

我有一个简单的结构化元素:html(带有 Bootstrap)css 和 js 文件。我创建了 React.js 元素(通过 npm 中的 create-react-app)。我已经安装了 Bootstrap、JQuery 等。问题是,当我将我的 html 文件翻译成 jsx 时,React 似乎忽略了样式(我在 React 中正常应用)。它忽略了网格。我有 3 行,其中一行的左侧有一个元素,每行有两行,有 3 个元素(col-sm-4 col-md-4 col-lg4)。在我的旧元素(纯 html、css、js)中,它工作正常,但在这个 React 元素中,它的行为就像……它会在移动版本中(每行一个元素在另一个元素下面)。同时使用开发工具,我看到浏览器代码中仍然存在此行和列,但样式为空......你能告诉我发生了什么事吗?我应该在哪里寻找这个问题的原因?

最佳答案

如果您只是使用标准 React,您需要在 <head></head> 中包含以下内容标签内 public/index.html :

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

如果您使用的是 Webpack 等构建器,请运行 npm install bootstrapinclude src/index.js 中的依赖项.

假设所有这些都正确完成,设置 React 组件样式的正确方法如下:

直接给标签设置样式:

<div style={{margin: '10px'}}>This is a div!</div>

使用 Bootstrap 类的样式:

<button className="btn btn-primary">Click me!<button>

有用的链接:

https://getbootstrap.com/docs/4.1/getting-started/introduction/

https://blog.logrocket.com/how-to-use-bootstrap-with-react-a354715d1121

关于javascript - 将 html bootstrap 转换为 react.js jsx 样式后更改 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53657615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com