简体   繁体   中英

How to use function outside of class component in React

I am tying to understand function components in react, but not getting a real picture. Here I took some code and modified it for my own understanding. Any help.

React:

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;
   }  

Just trying to understand and figure out a solution i think you can go for this

class App extends Component {

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

export default App;

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