简体   繁体   English

反应本机 - 将状态分配给 const

[英]react native - assigning state to const

I'm starting with react native, and when using a library called react native paper, I've come across a statement where the state is being assigned to a const as shown below.我从 react native 开始,当使用名为 react native paper 的库时,我遇到了一个语句,其中状态被分配给一个常量,如下所示。

import * as React from 'react';
import { Searchbar } from 'react-native-paper';

export default class MyComponent extends React.Component {
  state = {
    firstQuery: '',
  };

  render() {
    const { firstQuery } = this.state;
    return (
      <Searchbar
        placeholder="Search"
        onChangeText={query => { this.setState({ firstQuery: query }); }}
        value={firstQuery}
      />
    );
  }
}

Beginning of the 'Render' method, you could see const { firstQuery } = this.state; 'Render' 方法的开头,你可以看到 const { firstQuery } = this.state; Could someone please explain why the state is being assigned to a const named 'firstQuery', and even if it have a reason, how will the assignment correctly map the property 'firstQuery' inside the state object to the const ?有人可以解释为什么将状态分配给名为“firstQuery”的常量,即使它有原因,分配如何将状态对象内的属性“firstQuery”正确映射到 const ?

Thanks in advance.提前致谢。 The code sample is from https://callstack.github.io/react-native-paper/searchbar.html#value代码示例来自https://callstack.github.io/react-native-paper/searchbar.html#value

That syntax is not React nor React Native.该语法既不是 React 也不是 React Native。 It's just Javascript's syntax, called destructuring.这只是 Javascript 的语法,称为解构。

const { firstQuery } = this.state;

is equivalent to相当于

const firstQuery = this.state.firstQuery;

just a short-hand shortcut syntax, you see 2 firstQuery s ?只有很短的手快捷语法,你看到2 firstQuery S' People just don't want duplication in code, so they invented it.人们只是不想在代码中重复,所以他们发明了它。


See the vanilla javascript snippet below:请参阅下面的vanilla javascript片段:

 const object = { name: 'Aby', age: 100, } const { name, age } = object; // instead of // const name = object.name; console.log(name, age); console.log(object.name, object.age); //========================================= // imagine: const obj = { veryLongPropertyNameToType: 420 } const { veryLongPropertyNameToType } = obj; // instead of // const veryLongPropertyNameToType = obj.veryLongPropertyNameToType;

Like another answer mentioned, it's just JavaScript syntax aka destructuring.就像提到的另一个答案一样,它只是 JavaScript 语法,也就是解构。 If you're feeling confused and wished to just use the "vanilla" JavaScript syntax, you can take a look at below.如果您感到困惑并希望只使用“vanilla”JavaScript 语法,您可以查看下面的内容。

import * as React from 'react';
import { Searchbar } from 'react-native-paper';

export default class MyComponent extends React.Component {
  state = {
    firstQuery: '',
  };

  render() {
    return (
      <Searchbar
        placeholder="Search"
        onChangeText={query => { this.setState({ firstQuery: query }); }}
        value={this.state.firstQuery} // <<<<<<<<<<< LOOK HERE
      />
    );
  }
}

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

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