Breakpoints from regular bootstrap(not react-bootstrap) doesnt work in my react app.

huangapple go评论75阅读模式
英文:

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

&lt;div
   className=&quot;row&quot;
&gt;
              {data!== undefined &amp;&amp; data.length !== 0 ? (
                data.map((el) =&gt; (
                  &lt;div className=&quot;col col-lg-2 col-md-6 g-3&quot; key={el._id}&gt;
                    &lt;div className=&quot;card&quot;&gt;
                      &lt;div className=&quot;card-body&quot;&gt;
                        &lt;p className=&quot;card-text fw-bold fs-5&quot;&gt;{el.name}&lt;/p&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;
                ))
              ) : (
                &lt;p&gt;no data&lt;/p&gt;
              )}
            &lt;/div&gt;

Breakpoints from regular bootstrap(not react-bootstrap) doesnt work in my react app.

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

{
  &quot;name&quot;: &quot;stackblitz-starters-acx6ws&quot;,
  &quot;version&quot;: &quot;0.0.0&quot;,
  &quot;private&quot;: true,
  &quot;dependencies&quot;: {
    &quot;@popperjs/core&quot;: &quot;^2.11.7&quot;,
    &quot;@types/react&quot;: &quot;18.2.15&quot;,
    &quot;@types/react-dom&quot;: &quot;18.2.7&quot;,
    &quot;bootstrap&quot;: &quot;^5.3.0&quot;,
    &quot;react&quot;: &quot;18.2.0&quot;,
    &quot;react-dom&quot;: &quot;18.2.0&quot;
  },
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;react-scripts start&quot;,
    &quot;build&quot;: &quot;react-scripts build&quot;,
    &quot;test&quot;: &quot;react-scripts test --env=jsdom&quot;,
    &quot;eject&quot;: &quot;react-scripts eject&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;react-scripts&quot;: &quot;latest&quot;
  }
}

index.tsx

import * as React from &#39;react&#39;;
import { StrictMode } from &#39;react&#39;;
import { createRoot } from &#39;react-dom/client&#39;;
import &#39;bootstrap/dist/css/bootstrap.css&#39;;

import App from &#39;./App&#39;;

const rootElement = document.getElementById(&#39;root&#39;);
const root = createRoot(rootElement);

root.render(
  &lt;StrictMode&gt;
    &lt;App /&gt;
  &lt;/StrictMode&gt;
);

App.tsx

import * as React from &#39;react&#39;;
import &#39;./style.css&#39;;

export default function App() {
  return (
    &lt;div&gt;
      &lt;h1&gt;Hello StackBlitz!&lt;/h1&gt;
      &lt;p&gt;Start editing to see some magic happen :)&lt;/p&gt;
      &lt;button className=&quot;btn btn-success&quot;&gt;Success&lt;/button&gt;

      &lt;div className=&quot;row&quot;&gt;
        &lt;div className=&quot;col col-lg-2 col-md-6 g-3&quot;&gt;
          &lt;div className=&quot;card&quot;&gt;
            &lt;div className=&quot;card-body&quot;&gt;
              &lt;p className=&quot;card-text fw-bold fs-5&quot;&gt;Card&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div className=&quot;col col-lg-2 col-md-6 g-3&quot;&gt;
          &lt;div className=&quot;card&quot;&gt;
            &lt;div className=&quot;card-body&quot;&gt;
              &lt;p className=&quot;card-text fw-bold fs-5&quot;&gt;Card&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div className=&quot;col col-lg-2 col-md-6 g-3&quot;&gt;
          &lt;div className=&quot;card&quot;&gt;
            &lt;div className=&quot;card-body&quot;&gt;
              &lt;p className=&quot;card-text fw-bold fs-5&quot;&gt;Card&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

答案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 &amp;&amp; ( 
    data.map((el) =&gt; (
        &lt;div className=&quot;col col-lg-2 col-md-6 g-3&quot; key={el._id}&gt;
            &lt;div className=&quot;card&quot;&gt;
                &lt;div className=&quot;card-body&quot;&gt;
                   &lt;p className=&quot;card-text fw-bold fs-5&quot;&gt;{el.name}&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
         &lt;/div&gt;
)}

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.

huangapple
  • 本文由 发表于 2023年7月18日 16:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定