Material-ui: @ material-ui / styles:クラスコンポーネントでの使用法?

作成日 2019年05月24日  ·  17コメント  ·  ソース: mui-org/material-ui

クラスコンポーネント内でmakeStyles APIをどのように使用しますか? ドキュメントには、関数コンポーネントの例のみが示されています。

これをクラスコンポーネントのrenderメソッド内で使用しようとすると、次のエラーが発生します。

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
docs question

最も参考になるコメント

私はこのようにmakeStyle代わりにwithStyles使用しました...

import React from 'react';
import { withStyles } from '@material-ui/core/styles';

const styles = (theme) => ({
  fab: {
    position: 'fixed',
    bottom: theme.spacing(2),
    right: theme.spacing(2),
  },
});

class LoginComponent extends React?Component {
  render() {
    const { classes } = this.props;
    /** your UI components... */
  }
}

export default withStyles(syles)(LoginComponent);

全てのコメント17件

クラスコンポーネント内でフックを使用することはできません。 機能コンポーネントが必要であるか、高階( withStyles )を使用する必要があります。

最後になりますが、同様の報告があれば、それを文書化する必要があると思います。

@oliviertassinari :しかし、アプリがクラスとして宣言されている場合、どうすればよいですか?
アプリには状態やその他の機能があるからです。

@ Angelk90高次のコンポーネントAPIを使用する必要があります: httpswithStyles )。

ドキュメントによると、 withStyles apiの使用例はまだ関数コンポーネントです。

https://material-ui.com/styles/basics/

だから私はあなたの答えに反対票を投じました。

@oliviertassinari
HOCでmakeStylesを使用するにはどうすればよいですか?
私のユースケースによると、私はスタイルのマテリアルテーマオブジェクトが必要です。

@ ghosh-jaideepによって提案されているように、
次のようなテーマユーティリティを使用する可能性を持たせる方法:

[theme.breakpoints.up("md")]: {
          paddingLeft: theme.spacing(2),
          paddingTop: theme.spacing(2)
}

クラスコンポーネント内?
ありがとう

私はこのようにmakeStyle代わりにwithStyles使用しました...

import React from 'react';
import { withStyles } from '@material-ui/core/styles';

const styles = (theme) => ({
  fab: {
    position: 'fixed',
    bottom: theme.spacing(2),
    right: theme.spacing(2),
  },
});

class LoginComponent extends React?Component {
  render() {
    const { classes } = this.props;
    /** your UI components... */
  }
}

export default withStyles(syles)(LoginComponent);

@ Ess-Soft LoginComponentは親コンポーネントからどのようにレンダリングされますか?

  • テーマを小道具として渡しましたか?
  • 親コンポーネントでThemeProviderを使用しましたか?

親コンポーネントのコードスニペットを共有すると、さらに便利になります。

@ Ess-Softと同じことを行い、withStylesの前にwidthThemeを追加しました
withTheme(withStyles(useStyles)(LoginComponent ))

そうすれば、 render内でもthemeを使用できます

誰かがスタイルのことで説明できますか? 私は正直に言うと、このようなものを見たとき、私は笑が男に来ると思いますAngularははるかに正常です。 今日3時間、答えを探して探していたら、これを見つけました。

@ xtianus79このAPIは、クラスコンポーネントでも機能します: https//material-ui.com/styles/basics/#higher-order-component-api

@oliviertassinariグリッドでの例を示していただけますか? 笑それは私がやろうとしていることであり、私はすぐそこに立ち往生しています。 makeStylesとcreateStylesを使用していますが、関数なしでそれを行う方法がわかりませんか? あなたはそれを手伝ってくれますか。

@ xtianus79StackOverflowでさらにヘルプが見つかります

その特定の問題とユースケースについては@oliviertassinariが表示されません。

うーん。 それでもhttps://material-ui.com/styles/basics/#higher-order-component-apiは「テーマ」の使用方法に答えていませんか? フックで機能コンポーネントのみをサポートするのはなぜですか? 人々はレガシーコードやクラスベースのコンポーネントの必要性を持っています。

@DewangS withTheme() APIを参照してください: https ://material-ui.com/styles/api/#withtheme-component-component。

このページは役に立ちましたか?
0 / 5 - 0 評価