#003 React component inherits a custom component

Source: Internet
Author: User

Topic: React components inherit custom components I. Requirements description

Description: There are a,b,c,d four components, there are some common logic, such as setting data, get data, there are some common properties, do not want to write these attributes in each component, how to do? "and the object-oriented language, C#,java's base class idea is the same"

If something is common, there are some ways that you can use React's Mixins (ES5), high-order components (ES6) "High- order functions are not well understood, how to use, to find the information "

But if there's a common attribute, then it's a little too much.

In the React, do you think you can inherit a custom component?
After searching for information, it is found that React is a component that can inherit its own definition.

Second, the solution

The steps to implement are simple, just

 
   
  
  1. class Win extends React.Component

Replaced by

 
   
  
  1. class Win extends BaseWin
1. Example code
  
 
  1. import React from ‘react‘
  2. /**
  3. * 所有弹框的基类
  4. */
  5. class BaseWin extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.name = ‘zhongxia‘;
  9. this.state = {};
  10. }
  11. common() {
  12. alert(‘this is a common function!‘)
  13. }
  14. }
  15. export default BaseWin;
  
 
  1. import React from ‘react‘
  2. import BaseWin from ‘./baseWindow‘
  3. class Win extends BaseWin {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. ...this.props
  8. };
  9. console.log(this.name);
  10. this.common();
  11. }
  12. getData() {
  13. return this.state;
  14. }
  15. render() {
  16. this.state.node.model.set({name: ‘zhongxia‘, age: 17})
  17. return (
  18. <div className="pop-dialog">
  19. <h2>弹框1</h2>
  20. <form>
  21. <label htmlFor="">用户名:</label><input value={this.state.name} type="text"/>
  22. <label htmlFor="">密码:</label><input type="password" value={this.state.password}/>
  23. </form>
  24. </div>
  25. );
  26. }
  27. }
  28. export default Win;
2. Instantiate the order of the React components and

Instantiate Subclass Component = = constructor inside super (prop) to instantiate the parent class component = = "Parent Component instantiation End = =" Subclass Component Instantiation End

Run by:

1. Sub-class constructors

2. Super (props) instantiation of the parent class

3. The subclass constructor ends and you can already get the properties and methods of the parent class

4. Subclass instances Start rendering the page



From for notes (Wiz)

#003 React component inherits a custom component

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.