英文:
Breakpoints from regular bootstrap(not react-bootstrap) doesnt work in my react app
问题
我有这个JSX
<div className="row">
{data !== undefined && data.length !== 0 ? (
data.map((el) => (
<div className="col col-lg-2 col-md-6 g-3" key={el._id}>
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">{el.name}</p>
</div>
</div>
</div>
))
) : (
<p>没有数据</p>
)}
</div>
尝试调整浏览器窗口大小,卡片不会移动,你可以看到这张图片。为什么会发生这种情况?我该如何修复?
英文:
I have this jsx
<div
className="row"
>
{data!== undefined && data.length !== 0 ? (
data.map((el) => (
<div className="col col-lg-2 col-md-6 g-3" key={el._id}>
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">{el.name}</p>
</div>
</div>
</div>
))
) : (
<p>no data</p>
)}
</div>
Tried to resize my browser window and the cards didnt move, you can see the image. Why does this happen? How can I fix this?
答案1
得分: 2
以下是要翻译的内容:
以下代码有效。我猜您没有在React项目中正确包含Bootstrap。
查看在线演示。
package.json
{
"name": "stackblitz-starters-acx6ws",
"version": "0.0.0",
"private": true,
"dependencies": {
"@popperjs/core": "^2.11.7",
"@types/react": "18.2.15",
"@types/react-dom": "18.2.7",
"bootstrap": "^5.3.0",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"react-scripts": "latest"
}
}
index.tsx
import * as React from 'react';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import 'bootstrap/dist/css/bootstrap.css';
import App from './App';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
root.render(
<StrictMode>
<App />
</StrictMode>
);
App.tsx
import * as React from 'react';
import './style.css';
export default function App() {
return (
<div>
<h1>Hello StackBlitz!</h1>
<p>Start editing to see some magic happen :)</p>
<button className="btn btn-success">Success</button>
<div className="row">
<div className="col col-lg-2 col-md-6 g-3">
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">Card</p>
</div>
</div>
</div>
<div className="col col-lg-2 col-md-6 g-3">
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">Card</p>
</div>
</div>
</div>
<div className="col col-lg-2 col-md-6 g-3">
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">Card</p>
</div>
</div>
</div>
</div>
</div>
);
}
英文:
The following code works. I guess you didn't include Bootstrap correctly in your React project.
See the live demo.
package.json
{
"name": "stackblitz-starters-acx6ws",
"version": "0.0.0",
"private": true,
"dependencies": {
"@popperjs/core": "^2.11.7",
"@types/react": "18.2.15",
"@types/react-dom": "18.2.7",
"bootstrap": "^5.3.0",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"react-scripts": "latest"
}
}
index.tsx
import * as React from 'react';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import 'bootstrap/dist/css/bootstrap.css';
import App from './App';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
root.render(
<StrictMode>
<App />
</StrictMode>
);
App.tsx
import * as React from 'react';
import './style.css';
export default function App() {
return (
<div>
<h1>Hello StackBlitz!</h1>
<p>Start editing to see some magic happen :)</p>
<button className="btn btn-success">Success</button>
<div className="row">
<div className="col col-lg-2 col-md-6 g-3">
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">Card</p>
</div>
</div>
</div>
<div className="col col-lg-2 col-md-6 g-3">
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">Card</p>
</div>
</div>
</div>
<div className="col col-lg-2 col-md-6 g-3">
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">Card</p>
</div>
</div>
</div>
</div>
</div>
);
}
答案2
得分: 1
这里有一段示例的更干净的代码:
{ data.length && (
data.map((el) => (
<div className="col col-lg-2 col-md-6 g-3" key={el._id}>
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">{el.name}</p>
</div>
</div>
</div>
))}
如果数组为空或未定义,它将始终返回false,因此不需要添加其他条件,因此这应该起到相同的作用。
英文:
Just as a side note, here is a suggestive cleaner code:
{ data.length && (
data.map((el) => (
<div className="col col-lg-2 col-md-6 g-3" key={el._id}>
<div className="card">
<div className="card-body">
<p className="card-text fw-bold fs-5">{el.name}</p>
</div>
</div>
</div>
)}
If the array is empty or undefined, it will always turn false so there's no need to add the other conditions so this should work the same.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论