首页 > 解决方案 > ...属性在 react-native 功能组件中

问题描述

我在 react-native 应用程序中有以下功能组件。在第二行代码中,存在...attributes令人困惑的地方。虽然我知道它代表较新 JavaScript 版本中的传播语法,但我找不到它的attributes含义。如果是这样说..props,那就可以理解了。我试图谷歌但找不到任何合适的答案。

问题

attrributes下面第二行代码片段中的含义是什么?

  const Loader = (props) => {
  const { loading, loaderColor, loaderText, ...attributes } = props;

  return (
    <Modal
      transparent={true}
      animationType={'none'}
      visible={loading}
      onRequestClose={() => {
        console.log('close modal');
      }}>
      <View style={styles.modalBackground}>
        <View style={styles.activityIndicatorWrapper}>
          <ActivityIndicator
            animating={loading}
            color={loaderColor? loaderColor : '#0000ff'}
            size={Platform.OS === 'ios' ? 'large' : 75}
          />
          {loaderText ? (
            <View style={{ flexDirection: 'row' }}>
              <Text style={styles.modalText}>{loaderText}</Text>
            </View>
          ) : (
            ''
          )}
        </View>
      </View>
    </Modal>
  );
};

标签: javascriptreact-nativespread-syntax

解决方案


attributes是新创建的常量的名称,其中包含来自 的其余属性props

const {
  a,
  b,
  ...objectContainingEveryOtherPropertyExceptAB
} = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
console.log(objectContainingEveryOtherPropertyExceptAB);

所以如果你像这样使用你的组件,<Loader loading foo="bar" />那么attributes将等于{ foo: 'bar' }


推荐阅读