我试图在反应性js上创建秒表。以下是我的代码
< div id =“root”>< / div>< script src =“https://unpkg.com/react@16.3.1/umd/react.development.js”>< / …
您的功能组件接收道具作为第一个参数。
对象解构应该是这样的
<script type="text/babel"> function StopWatch({running,lapse}) { const buttonStyles = { border: '1px solid #ccc',background: '#fff',fontSize: '2em',padding: 15,margin: 5,width: 200, } return( <div> <label style={{ fontSize: '5em',display: 'block', }}>{lapse}ms </label> <button style={buttonStyles}>{running ? 'stop' : 'start'}</button> <button style={buttonStyles}>Clear</button> </div> ) } const rootElement = document.getElementById("root") const element = <StopWatch running={true} lapse={0} /> ReactDOM.render(element,rootElement) </script>