Why is materialize not working with reactjs

2019-08-12 23:23发布

问题:

I created a static web page with materialize.css library, when I tried to move some components to react but they are not working.

This is my html boilerplate

<!DOCTYPE html>
<html>
  <head>
    <!--Import materialize.css-->
    <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>
    <!--Let browser know website is optimized for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <script src="http://fb.me/react-with-addons-0.13.1.js"></script>
    <script src="http://fb.me/JSXTransformer-0.13.1.js"></script>
  </head>
  <body>
    <div id="button"></div>
    <script type=text/jsx src="main.js"></script>

    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="js/materialize.min.js"></script>
  </body>
</html>

and this is main.js

var ButtomTest = React.createClass({
  render: function() {
    return (
      <div className="test">
        <a class="waves-effect waves-light btn">Stuff</a>
      </div>
    );
  }
});
React.render(
  <ButtomTest />,
  document.getElementById('button')
);

In the browser i only see "Stuff" in green color. What am I doing wrong?

回答1:

First, ensure that you are using className instead of class attribute in your jsx code.

Second, materialize.css uses wave.js to create a wave effect on button clicks and it doesn't fit quite well with react. But you can use material-ui`s Ripple components achieve the same effect



回答2:

I've actually had an easier time working with materializecss.com than any of the other tool kits. However, I do this:

1) I do load the css via link at the main page, just because it's easier and doesn't seem to cause conflicts with other tools

2) So far, anytime anything doesn't work it's because I typed "class" instead of "className" (as you did in your example)

3) Sometimes you have to call the Materialize javascript object. For example:

Materialize.Toast("a popup says what?",4000);

Luckily, because of how I load it I only have to amend that to:

window.Materialize.Toast("a popup says what?",4000);

My whole UI framework is simply materializecss.com + Mobx.
It's working out pretty well so far.