No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Web development

Antd Basic – Controlled and Uncontrolled components

by npn
August 24, 2020
in Web development, Ant Design tutorial, The Basics
Reading Time: 5 mins read
0
0
SHARES
2.2k
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 Uncontrolled components
  • 2 Controlled component
    • 2.1 Tabs component
    • 2.2 Tree component
Rate this post

By reading the post Antd Basic – Create first ReactJs Component, You should understand the difference between props and state. In this post, we will introduce the two concepts of “controlled components” and “uncontrolled components“.

Uncontrolled components

Look at this simple example, there is an input component.

const MyInput = ({ onChange }) => (
  <input onChange={onChange} />
);

The above component will display an input box, every time there is user input, it will call the passed parameter onChange.


Next, we will use the MyInput component in another component:

class Demo extends React.Component {
  onTextChange = (event) => {
    console.log(event.target.value);
  }

  render() {
    return (
      <MyInput onChange={this.onTextChange} />
    );
  }
}

In the above code, we encapsulate the MyInput component with a listener function onTextChange.


Now, a reset button is needed. When clicking it, the content of MyInput can be cleared. Then you can adjust it as follows.

<div>
  <MyInput onChange={this.onChange} />
  <button onClick={this.onTextReset}>Reset</button>
</div>
onTextReset = () => {
  // What will we do next?
  // Get the input element inside MyInput and set the value to it
}

It is not easy to modify the value of the MyInput component.


For such components that cannot directly control the state, we call them “uncontrolled components“.

ADVERTISEMENT

Controlled component

Next, we make some adjustments. Transform it into a controlled component.

const MyInput = ({ value = '', onChange }) => (
  <input value={value} onChange={onChange} />
);

At this time, the input of MyInput is completely determined by the value attribute.


You will find that you cannot enter anything in the input box with the new code (because the value is always empty).


Let’s modify the demo to make it work again:

class Demo extends React.Component {
  state = {
    text: '',
  }

  onTextChange = (event) => {
    this.setState({ text: event.target.value });
  }

  render() {
    return (
      <MyInput value={this.state.text} onChange={this.onTextChange} />
    );
  }
}

Well, resetting becomes a breeze:

onTextReset = () => {
  this.setState({ text: '' });
}

The two concepts of “controlled” and “uncontrolled” differ in whether the state of this component can be modified externally.

A properly designed component should support both “controlled” and “uncontrolled” forms, that is, when the developer does not control the properties of the component, the component manages its own state, and when the developer controls the properties of the component, the component should be controlled by the properties control.

The development of a complex component needs to pay more attention to this point, to avoid that only some of the properties are controlled, making it a semi-controlled component.

Tabs component

For a typical component example, you can refer to the Antd tabs component:

<Tabs>
  <TabPane tab="Tab 1" key="1">Content of Tab Pane 1</TabPane>
  <TabPane tab="Tab 2" key="2">Content of Tab Pane 2</TabPane>
</Tabs>

In most cases, developers don’t need to consider how to control which tab page the tabs stay on, and users can click on them when needed. In this case, tabs will run as “uncontrolled components“.
When the activeKey attribute is passed, the tabs component will be transformed into a “controlled component“. Label switching needs to be controlled by code:

<Tabs activeKey={this.state.activeKey} onChange={this.onTabChange}>
  <TabPane tab="Tab 1" key="1">Content of Tab Pane 1</TabPane>
  <TabPane tab="Tab 2" key="2">Content of Tab Pane 2</TabPane>
</Tabs>
state = {
  activeKey: '1',
}

onTabChange = (activeKey) => {
  this.setState({ activeKey });
}

Tree component

By controlling the state of the component, we can realize some functions that the original component design did not realize.
For example, in the tree component. We expand/close by clicking the small triangle on the left of the node, and clicking the text part selects the node:

<Tree>
  <TreeNode title="parent 1" key="0-0">
    <TreeNode title="leaf" key="0-0-0" />
    <TreeNode title="leaf" key="0-0-1" />
  </TreeNode>
</Tree>

If we want to change to click on the text part, and also expand/close the node, what should we do?

First, we need to query the document to find out what attributes are related to this requirement.
• expandedKeys: set expanded nodes
• selectedKeys: set the selected node
• onExpand: trigger when the node is expanded/closed
• onSelect: trigger when the node is selected
It’s easy to think of how to adjust: when the node is selected, change the original selectedKeys to update expandedKeys.

Converted into the corresponding code:

<Tree
  expandedKeys={this.state.expandedKeys}
  selectedKeys={[]}
  onExpand={this.onExpand}
  onSelect={this.onSelect}
>
  <TreeNode title="parent 1" key="0-0">
    <TreeNode title="leaf" key="0-0-0" />
    <TreeNode title="leaf" key="0-0-1" />
  </TreeNode>
</Tree>
state = {
  expandedKeys: [],
}
// Receive the original expansion event and record expandedKeys in the state
onExpand = (expandedKeys) => {
  this.setState({ expandedKeys });
}
// Receive selected event, modify expandedKeys
onSelect = (selectedKeys) => {
  const { expandedKeys } = this.state;
  const key = selectedKeys[0];
  if (expandedKeys.includes(key)) {
    // Remove key
    this.setState({
      expandedKeys: expandedKeys.filter(k => k !== key),
    });
  } else {
    // Add key
    this.setState({ expandedKeys: [...expandedKeys, key] });
  }
}
Tags: Antdesign componentsControlled ComponentsUncontrolled components
ShareTweetShare
Previous Post

Antd Basic – How to use Ant Design components

Next Post

AntD Basic layout – Create Website layout with Ant Design

npn

Related Posts

Linux

What is the location of the MySQL databases?

April 24, 2022
598
Web development

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
492
Web development

Gmail – Gmail Sign Up – Gmail Login

August 30, 2021
7.1k
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
1.3k
Javascript

How does Nodejs solve the problem of high concurrency?

July 18, 2021
1.3k
Linux

How to create a self-signed SSL certificate for Apache on Ubuntu 16.04

July 18, 2021
1k
Next Post

AntD Basic layout - Create Website layout with Ant Design

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
Exit mobile version