1

我是反应js的新手。最近我浏览了 react js文档和 react-pos文档。我已经实现了以下代码段。但是当我运行它时,它在浏览器中没有任何影响。我哪里错了?

<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>
4

2 回答 2

0

也许是这样:

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

无论哪种方式,您都需要使用this.state.isVisible,而不仅仅是 isVisible..

于 2019-03-14T12:46:25.433 回答
0

如果您babel-standalone用于实验,则需要确保type将脚本的属性设置为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>

于 2019-03-14T12:47:25.270 回答