简体   繁体   English

如何通过React-Native中的React-Navigation多次传递一个参数?

[英]How to pass a paramater multiple times through React-Navigation in React-Native?

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>
    );
}

I currently am already passing the value 'level' to the TestFunc parameter, but now I wish to pass the same parameter to the screen I navigate to when I press the TestButton component.我目前已经将值“level”传递给 TestFunc 参数,但现在我希望将相同的参数传递给按下 TestButton 组件时导航到的屏幕。

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

This documentation shows me how I would do such with a newly initialized parameter, but it doesn't seem to work with parameters that have been passed from previous screens.该文档向我展示了如何使用新初始化的参数执行此操作,但它似乎不适用于从先前屏幕传递的参数。

Any suggestions?有什么建议么?

EDIT: I have tried to make it a little bit clearer.编辑:我试图让它更清楚一点。 I want to go from function A to B to C, all different components/screens in my app.我想要 go 从 function A 到 B 到 C,我的应用程序中的所有不同组件/屏幕。 I want to take the 'level' that I obtain from function A and pass it from B to C. Currently I am correctly retrieving the 'level' value in function B, however I can not seem to pass it to function C.我想获取从 function A 获得的“级别”并将其从 B 传递给 C。目前我正在正确检索 function B 中的“级别”值,但是我似乎无法将其传递给 function C。

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>
  );
}

You can get the all props on Category component also您还可以获得 Category 组件上的所有道具

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

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

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