【问题标题】:How to pass a paramater multiple times through React-Navigation in React-Native?如何通过 React-Native 中的 React-Navigation 多次传递参数?
【发布时间】:2020-08-18 23:14:36
【问题描述】:
function A() {
  const [levels, setLevels] = React.useState(null);

  // upon the component mount, we call the database.
  React.useEffect(() => {
    database()
      .ref()
      .once("value")
      .then((snapshot) => {
        //get Levels from db
        const levels = snapshot.map((child) => child.key);
        setLevels(levels);
      });
  }, []);

  return (
          <View>
            {levels.map((element) => {
              return <B level={element} />;
            })}
          </View>
  );
}
function B(props){    
    const navigation = useNavigation();
    return (
        <View>

        <TestButton
            title={props.level}
            onPress={() => navigation.navigate('C')}
        />

        </View>
    );
}

我目前已经将值 'level' 传递给 TestFunc 参数,但现在我希望将相同的参数传递给我按下 TestButton 组件时导航到的屏幕。

https://reactnavigation.org/docs/params/

本文档向我展示了如何使用新初始化的参数执行此操作,但它似乎不适用于从之前的屏幕传递的参数。

有什么建议吗?

编辑: 我试图让它更清楚一点。我想从功能 A 到 B 再到 C,我的应用程序中的所有不同组件/屏幕。我想获取从函数 A 获得的“级别”并将其从 B 传递给 C。目前我正在正确检索函数 B 中的“级别”值,但是我似乎无法将其传递给函数 C。

【问题讨论】:

  • 你是怎么传参数的可以看看吗?
  • 您希望将参数从屏幕 A 发送到屏幕 B 或从屏幕 B 发送到屏幕 A ??
  • 对不起,我已经编辑了帖子以使其更清晰。

标签: javascript android react-native mobile react-navigation


【解决方案1】:
function TestFunc (props){    
const navigation = useNavigation();
return (
    <View>

    <TestButton
        title={props.level}
        onPress={() => navigation.navigate('Category',{...props})}
       //{...props} send all props that's exist in props argument 
    />

    </View>
  );
}

您也可以获取 Category 组件上的所有道具

const { level } = this.props.navigation.state.params;

【讨论】:

  • 我得到 Component Exception: undefined is not an object,在检索 Category 组件中的 props 时评估“this.props”。如果我只尝试 props.params,我会得到同样的错误。
  • 没关系,在 Category 组件中将其从 'props' 更改为 'route' 并通过 const {level} = route.params; 检索它解决了我的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 2017-12-19
  • 1970-01-01
相关资源
最近更新 更多