如何在React JS中同时使用Chakra Provider和Material UI?

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

how to use chakra provider and material UI together in React JS?

问题

我们正在克隆flipkart.com项目。我们是一个团队,所以我们分配了工作。有一个人使用Material UI制作了首页,而我正在使用Chakra UI制作产品页面,但在合并后,应用程序无法正常工作,并且产生了不熟悉的错误。

所以,请提供一些解决方案,以便我们能够完成我们的演示。

在index.js中,我编写了这段代码

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Provider } from 'react-redux';
import FlipkartStore from './Components/Redux_Implementation/Store/Store';
import { BrowserRouter } from 'react-router-dom';
import { ThemeProvider } from '@material-ui/core/styles';
import { createTheme } from '@material-ui/core/styles';
import { ChakraProvider, extendTheme } from '@chakra-ui/react';

const muiTheme = createTheme();
const theme = extendTheme();

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider store={FlipkartStore}>
    <ChakraProvider theme={theme}>
      <ThemeProvider theme={muiTheme}>
        <BrowserRouter>
          <App />
        </BrowserRouter>
      </ThemeProvider>
    </ChakraProvider>
  </Provider>
);

// 如果您想要在应用程序中开始测量性能,请传递一个记录结果的函数
// 例如:reportWebVitals(console.log))
reportWebVitals();

显示以下错误:

如何在React JS中同时使用Chakra Provider和Material UI?

如果我移除ChakraProvider,页面可以工作,但我的产品页面CSS不起作用。

如何在React JS中同时使用Chakra Provider和Material UI?

英文:

We are working on project of clone of flipkart.com.We are working on team.so we have distributed our works. One of guy worked on landing page with material UI and i am working on product page with chakra ui but after merging app is not working and producing unfamiliar error.

So, provide some solution so we can make our presentation.

in index.js i have written this code

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import &#39;./index.css&#39;;
import App from &#39;./App&#39;;
import reportWebVitals from &#39;./reportWebVitals&#39;;
import { Provider } from &#39;react-redux&#39;;
import FlipkartStore from &#39;./Components/Redux_Implementation/Store/Store&#39;;
import { BrowserRouter } from &#39;react-router-dom&#39;;
import { ThemeProvider } from &#39;@material-ui/core/styles&#39;;
import { createTheme } from &#39;@material-ui/core/styles&#39;;
import { ChakraProvider, extendTheme } from &#39;@chakra-ui/react&#39;;

const muiTheme = createTheme();
const theme = extendTheme();

const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;Provider store={FlipkartStore}&gt;
    &lt;ChakraProvider theme={theme} &gt;
    &lt;ThemeProvider theme={muiTheme}&gt;
      &lt;BrowserRouter&gt;
        &lt;App /&gt;
      &lt;/BrowserRouter&gt;
    &lt;/ThemeProvider&gt;
    &lt;/ChakraProvider&gt;
  &lt;/Provider&gt;
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))

reportWebVitals();


its showing this error
如何在React JS中同时使用Chakra Provider和Material UI?

if am removing chakra provider page is working but my product page css is not working

如何在React JS中同时使用Chakra Provider和Material UI?

答案1

得分: 1

适用于我:

&lt;ThemeProvider theme={muiTheme}&gt;
  &lt;ChakraProvider theme={chakraTheme}&gt;
    ...
  &lt;/ChakraProvider&gt;
&lt;/ThemeProvider&gt;
英文:

Worked for me:

&lt;ThemeProvider theme={muiTheme}&gt;
  &lt;ChakraProvider theme={chakraTheme}&gt;
    ...
  &lt;/ChakraProvider&gt;
&lt;/ThemeProvider&gt;

答案2

得分: 0

你可以根据需要仅为你的页面包装 ChakraProvider

如果你正在使用 React Router,你可以仅为你的路径包装提供者。

示例:

<Switch>
  <Route path="/products">
    <ChakraProvider> /* &#128072; 仅为你的产品页面包装 */
      <Products />
    </ChakraProvider>
  </Route>
  <Route path="/">
    <Home />
  </Route>
</Switch>
英文:

You can wrap the ChakraProvider only for your page as needed.

If you are using React Router. You could wrap the provider only for your path.

Example:

&lt;Switch&gt;
  &lt;Route path=&quot;/products&quot;&gt;
    &lt;ChakraProvider&gt; /* &#128072; wrap only for your products page */
      &lt;Products /&gt;
    &lt;/ChakraProvider&gt;
  &lt;/Route&gt;
  &lt;Route path=&quot;/&quot;&gt;
    &lt;Home /&gt;
  &lt;/Route&gt;
&lt;/Switch&gt;

答案3

得分: 0

将你的路由包裹在ChakraProvider中。

<Switch>
    <Route path='/your_path'>
        <ChakraProvider>
            <YourComponent />
        </ChakraProvider>
    </Route>
    <Route path='/your_path_2' element={<YourOtherComponent />} />
</Switch>

只需将你的路由包裹在路由中。

英文:

wrap your route inside ChakraProvider.

&lt;Switch&gt;
      &lt;Route path=&#39;/your_path&#39;&gt;
           &lt;ChakraProvider&gt;
                &lt;YourComponent /&gt;
           &lt;/ChakraProvider&gt;
      &lt;/Route&gt;
      &lt;Route path =&#39;/your_path_2&#39; element={&lt;YourOtherComponent /&gt;} /&gt;
&lt;/Switch&gt;

Just wrap your route inside the route.

答案4

得分: 0

你想要使用的组件可以包装在 Chakra 提供程序中。

<Route path='/'>
   <ChakraProvider>
        <你想要的组件 />
   </ChakraProvider>
 </Route>
英文:

The component you want to use you can wrap it in chakra provider.

&lt;Route path=&#39;/&#39;&gt;
   &lt;ChakraProvider&gt;
        &lt;Component you want /&gt;
   &lt;/ChakraProvider&gt;
 &lt;/Route&gt;

<!-- end snippet -->

答案5

得分: 0

基本上,最佳的方法来同时使用 chakra-ui 和 mui 是合并它们提供的主题并将它们的提供者都给予相同的主题,像这样:

import "./styles.css";
import AutoComplete from "@material-ui/core/Autocomplete";
import TextField from "@material-ui/core/TextField";
import { ThemeProvider } from "@material-ui/core/styles";
import { deepmerge } from "@material-ui/utils";
import { createMuiTheme } from "@material-ui/core/styles";
import { ChakraProvider, extendTheme } from "@chakra-ui/react";

const muiTheme = createMuiTheme();
const chakraTheme = extendTheme();

//重要部分
const theme = deepmerge(chakraTheme, muiTheme);

console.log({ chakraTheme, muiTheme });

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <ChakraProvider theme={theme} resetCSS>
        <div className="App">
          <h1>Hello CodeSandbox</h1>
          <h2>Start editing to see some magic happen!</h2>
          <AutoComplete
            id="combo-box-demo"
            options={[{ title: "a" }, { title: "b" }, { title: "c" }]}
            getOptionLabel={(option) => option.title}
            renderInput={(params) => (
              <TextField {...params} label="Combo box" variant="outlined" />
            )}
          />
        </div>
      </ChakraProvider>
    </ThemeProvider>
  );
}
英文:

Basically the best way to use both chakra-ui and mui is to merge their provided theme and give both of their providers the same theme like this:

import &quot;./styles.css&quot;;
import AutoComplete from &quot;@material-ui/core/Autocomplete&quot;;
import TextField from &quot;@material-ui/core/TextField&quot;;
import { ThemeProvider } from &quot;@material-ui/core/styles&quot;;
import { deepmerge } from &quot;@material-ui/utils&quot;;
import { createMuiTheme } from &quot;@material-ui/core/styles&quot;;
import { ChakraProvider, extendTheme } from &quot;@chakra-ui/react&quot;;

const muiTheme = createMuiTheme();
const chakraTheme = extendTheme();

//the important part
const theme = deepmerge(chakraTheme, muiTheme);

console.log({ chakraTheme, muiTheme });

export default function App() {
  return (
    &lt;ThemeProvider theme={theme}&gt;
      &lt;ChakraProvider theme={theme} resetCSS&gt;
        &lt;div className=&quot;App&quot;&gt;
          &lt;h1&gt;Hello CodeSandbox&lt;/h1&gt;
          &lt;h2&gt;Start editing to see some magic happen!&lt;/h2&gt;
          &lt;AutoComplete
            id=&quot;combo-box-demo&quot;
            options={[{ title: &quot;a&quot; }, { title: &quot;b&quot; }, { title: &quot;c&quot; }]}
            getOptionLabel={(option) =&gt; option.title}
            renderInput={(params) =&gt; (
              &lt;TextField {...params} label=&quot;Combo box&quot; variant=&quot;outlined&quot; /&gt;
            )}
          /&gt;
        &lt;/div&gt;
      &lt;/ChakraProvider&gt;
    &lt;/ThemeProvider&gt;
  );
}

huangapple
  • 本文由 发表于 2023年1月8日 21:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048240.html
匿名

发表评论

匿名网友

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

确定