简体   繁体   中英

How to implement simple react js animation with react-pose without node js?

I am newbie in react js. Recently I went through the react js documentation and react-pos documentation . I have implemented the following snippet. But when I run it, it has no effect in the browser. Where am I wrong?

<html>
<head>
<style>
.box{
    background:red;
 }
</style>
</head>
<body>

<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"> 
</script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"> 
</script>
<script src="https://unpkg.com/react-pose/dist/react-pose.js"></script>

<div style="width:200px; height:200px; background:#eaeaea;" id="root"></div>

<script>
    const Box = posed.div({
      hidden: { opacity: 0 },
      visible: { opacity: 1 }
    });

    class Example extends React.Component {
      state = { isVisible: true };

      componentDidMount() {
        setInterval(() => {
          this.setState({ isVisible: !this.state.isVisible });
        }, 1000);
      }

      render() {
        const { isVisible } = this.state;
        return <Box className="box" pose={isVisible ? 'visible' : 'hidden'} 
  />;
      }
    }

    ReactDOM.render(<Example/>, document.getElementById('root'));
</script>

</body>
</html>

Perhaps this:

<Box className="box" style={{visible: this.state.isVisible, hidden: !this.state.isVisible}} />;

Either way, you need to use this.state.isVisible , not just isVisible..

If you use babel-standalone for experimentation, you need to make sure you set the type attribute of your script to text/babel .

 .box { width: 200px; height: 200px; background: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/react-pose@3.1.0/dist/react-pose.js"></script> <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"> </script> <div id="root"></div> <script type="text/babel"> const Box = posed.div({ hidden: { opacity: 0 }, visible: { opacity: 1 } }); class Example extends React.Component { state = { isVisible: true }; componentDidMount() { setInterval(() => { this.setState({ isVisible: !this.state.isVisible }); }, 1000); } render() { const { isVisible } = this.state; return <Box className="box" pose={isVisible ? "visible" : "hidden"} />; } } ReactDOM.render(<Example />, document.getElementById("root")); </script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM