繁体   English   中英

如何在 React 中的 class 组件之外使用 function

[英]How to use function outside of class component in React

我很想了解 function 组件的反应,但没有得到真实的画面。 这里我拿了一些代码,并根据自己的理解进行了修改。 任何帮助。

反应:

class App extends React.Component {
    state = { count: 0, add: 2 }

    handleIncrement() {
      this.setState({ count: this.state.count + 1 })
    }

    handleDecrement() {
       this.setState({ count: this.state.count - 1 })
     }

     anyMore() {
        this.setState({ add: this.state.add })
        this.setState({ count: this.state.count })
        any(add, count)
     }

    render() {
       return (
         <div>
             <button onClick={this.handleIncrement}>Increment by 1</button>
             <button onClick={this.handleDecrement}>Decrement by 1</button> <br />
             <button onClick={this.anyMore}>Add 2</button>
             <div>
                <p>&nbsp;{this.state.count} </p> <p>&nbsp; {this.state.add} </p>
             </div>
         </div>
    )
  }
}

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

  function any() {
     return a * b;
   }  

只是想了解并找出解决方案,我认为您可以为此拨打 go

class App extends Component {

render() {
    return (
        Any()
    );
}
}
const Any = () => {
return (
    <div>
        return a * b
    </div>
);
};

export default App;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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