javascript – 创建React组件的2种不同方法

前端之家收集整理的这篇文章主要介绍了javascript – 创建React组件的2种不同方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在关注一个反应教程,这是作者为创建一个基本的React组件而给出的示例代码
const React = require('react')
const ReactDOM = require('react-dom')

const App = () => {
    return (
        <div className='app-container'>
            <h1>Hello</h1>
        </div>
    )
}

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

他声称这是ES6.

但后来我看到了另一种创建组件的方法.

class App extends React.Component {
    render(){
        return <h1>Hello</h1>;

    }
}

嗯,我现在很困惑.有没有标准的做法做出反应?

解决方法

在React中,您可以创建所谓的有状态和无状态功能组件.无状态组件是简单的可重用组件,不需要维护状态.这是一个简短的演示( http://codepen.io/PiotrBerebecki/pen/yaoOKv),向您展示如何创建它们以及如何访问从父(有状态组件)传递的道具.

一个简单的例子可能是Facebook.com上的理论App状态组件.如果用户登录或注销,它可以维护状态以跟踪.然后在它的render()方法中,它将显示一个Loginlogout无状态按钮组件,向其传递当前状态.然后,Loginlogout无状态组件将显示

>如果用户登录,则为“登录”文本,或者
>如果用户登录,则为“注销”文本.

您可以在此处了解有关有状态与无状态组件的更多信息:ReactJS difference between stateful and stateless和此处React.createClass vs. ES6 arrow function

// Stateful component
class FacelookApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLoggedIn: false
    };
  }

  receiveClick() {
    this.setState({
      isLoggedIn: !this.state.isLoggedIn
    });
  }

  render() {
    return (
      <div>
        <h4>Welcome,I'm a stateful parent called Facelook App</h4>
        <p>I maintain state to monitor if my awesome user logged 
          in. Are you logged in?<br />
          <b>{String(this.state.isLoggedIn)}</b>
        </p><br />

        <p>Hi,we are three stateless (dumb) Loginlogout buttons 
          generated using different ES6 Syntax but having the same
          functionality. We don't maintain state. We will tell 
          our parent if the user clicks on us. What we render is 
          decided by the value of the prop sent to us by our parent.
        </p>

        <Loginlogout1 handleClick={this.receiveClick.bind(this)} 
                isLoggedIn={this.state.isLoggedIn}/>

        <Loginlogout2 handleClick={this.receiveClick.bind(this)} 
                isLoggedIn={this.state.isLoggedIn}/>

        <Loginlogout3 handleClick={this.receiveClick.bind(this)} 
                isLoggedIn={this.state.isLoggedIn}/>
      </div>
    );
  }
}


// Stateless functional components
// created in 3 equally valid ways
const Loginlogout1 = (props) => {
  return (
    <div>
      <button onClick={() => props.handleClick()}>
        Loginlogout v1 --- {props.isLoggedIn ? 'Log Out' : 'Log In'}
      </button>
    </div>
  );
};

// or
const Loginlogout2 = ({handleClick,isLoggedIn}) => (
    <div>
      <button onClick={() => handleClick()}>
        Loginlogout v2 --- {isLoggedIn ? 'Log Out' : 'Log In'}
      </button>
    </div>
);

// or
const Loginlogout3 = ({handleClick,isLoggedIn}) => {
  return (
    <div>
      <button onClick={() => handleClick()}>
        Loginlogout v3 --- {isLoggedIn ? 'Log Out' : 'Log In'}
      </button>
    </div>
  );
};



ReactDOM.render(
  <FacelookApp />,document.getElementById('app')
);

猜你在找的JavaScript相关文章