【问题标题】:React native How to pass data from function component to class componentReact Native 如何将数据从函数组件传递到类组件
【发布时间】:2021-05-20 16:25:38
【问题描述】:

我正在使用 react native functional 组件将数据传递给 基于类的组件,但我被卡住了,因为我无法检索类中的数据通过将 route.params.data 存储在目标组件内的新变量中,就像在基于函数的组件中一样。我必须使用基于特定类的组件的原因是我正在使用基于类组件概念的 MultiSelect react native future。 我像这样从功能组件传递数据:

navigation.navigate('Order', { screen: 'Order', initial: false, params: {data : user_id},
                                                                })

我应该在目标组件中执行此操作以获取传递的数据:

const info = route.params.data

但是没有得到基于类的组件中的数据,因为基于类的组件不能使用路由。那么请问我怎样才能达到这个目的?我也在使用 Redux,它已经在所有函数组件中发送了所有数据,但由于类组件无法使用 Redux 钩子。 非常感谢所有帮助。 注意: 我面临的限制是只能在基于类的组件中使用 React Native MultiSelect,它既不能从功能性父组件获取传递的值,也不能从痛处检索 redux 数据。 提前致谢。

【问题讨论】:

    标签: react-native function class


    【解决方案1】:

    multiselect.js

    import React from "react";
    import { View, SafeAreaView } from "react-native";
    import MultiSelect from "react-native-multiple-select";
    
    export default MultiSelectExample = (
      props /* getting the props from parent */
    ) => {
      // Create a selectedItems state, initialize it as empty array
      const [selectedItems, setSelectedItems] = React.useState([]);
      // Create a reference from the multiSelect
      const multiSelect = React.useRef(null);
    
      const onSelectedItemsChange = (items) => {
        setSelectedItems(items);
      };
    
      return (
        <SafeAreaView>
          <View>
            <MultiSelect
              hideTags
              items={props.items}
              uniqueKey="id"
              ref={(component) => {
                // assign the reference of the multiselect
                multiSelect.current = component;
              }}
              onSelectedItemsChange={onSelectedItemsChange}
              selectedItems={selectedItems}
              selectText="Pick Items"
              searchInputPlaceholderText="Search Items..."
              onChangeInput={(text) => console.log(text)}
              tagRemoveIconColor="#CCC"
              tagBorderColor="#CCC"
              tagTextColor="#CCC"
              selectedItemTextColor="#CCC"
              selectedItemIconColor="#CCC"
              itemTextColor="#000"
              displayKey="name"
              searchInputStyle={{ color: "#CCC" }}
              submitButtonColor="#CCC"
              submitButtonText="Submit"
            />
            {/* the '?' to check if a field in the object exists,
            at the first render of the screen the multiSelect.current is undefined
            so you need to check the existance of its properties or else it will throw
            an  error saying that undefined does not have function called getSelectedItemsExt*/}
            <View>{multiSelect.current?.getSelectedItemsExt?.(selectedItems)}</View>
          </View>
        </SafeAreaView>
      );
    };

    在 App.js 中

    import React from "react";
    import MultipleSelect from "./multiselect";
    
    const items = [
      {
        id: "92iijs7yta",
        name: "Ondo",
      },
      {
        id: "a0s0a8ssbsd",
        name: "Ogun",
      },
      {
        id: "16hbajsabsd",
        name: "Calabar",
      },
      {
        id: "nahs75a5sg",
        name: "Lagos",
      },
      {
        id: "667atsas",
        name: "Maiduguri",
      },
      {
        id: "hsyasajs",
        name: "Anambra",
      },
      {
        id: "djsjudksjd",
        name: "Benue",
      },
      {
        id: "sdhyaysdj",
        name: "Kaduna",
      },
      {
        id: "suudydjsjd",
        name: "Abuja",
      },
    ];
    
    export default App = () => {
      return <MultipleSelect items={items} />;
    };

    【讨论】:

    • 感谢您的建议。我尝试了this.props.navigation.params.data,结果“找不到未定义的属性'数据'。我尝试检查this.props.navigation.params属性。为此我console.log (this.props.navigation.params) 我收到结果为“未定义”,当我在 console.log(this.props.navigation) 收到 [object object]。我错过了什么?
    • @Prenam 不是您屏幕中的 MultiSelect 组件吗?那你为什么要使用类基组件?
    • 是的@Hamza Jadid,这里是文档 [MultiSelect]github.com/renrizzolo/…) 。我尝试在函数组件中使用它,但没有成功,因为 MultiSelect 的道具是建立在类概念之上的,例如我无法更改的 selectedItems={this.state.selectedItems}。你有办法在函数组件中使用它吗?
    • 问题是我使用导航挂钩将数据从函数组件传递到实现 MultiSelct 组件的类组件。解决方案是找到一种在功能组件内使用 MultiSelct 的方法,或者找到一种将参数从功能组件屏幕传递到类组件屏幕的方法。如果我必须在函数组件屏幕中使用 MultiSelct,&lt;View&gt; {this.multiSelect.getSelectedItemsExt(selectedItems)} &lt;/View&gt; 将如何写在函数组件中,因为现在它会触发错误,这很有意义。
    • 在之前的评论中我犯了一个错误,我使用的 MultiSelect doc 的实际 url 是 (npmjs.com/package/react-native-multiple-select)
    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 2021-07-18
    • 2020-09-23
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多