反应本机 - 按下按钮时,使用父组件加载整个屏幕



我正在尝试在按下按钮时从子组件加载我的父组件。但它不会从btnPress方法呈现父组件。我没有收到任何错误。

按钮按下

<Button onPress={() => btnPress(parent_id, id)}>
                <Icon name="arrow-forward" />
</Button>

btn新闻功能

function btnPress(parent_id, id) {
       const App = () => (
         //I have tried this way but this didn't work. No any error, i can see log on console
         <Container> 
           <Headerc headerText={'Fitness sdaf'} />
           <ExerciseList pId={parent_id} mId={id} />
         </Container>
       );  
         console.log(id);  
        AppRegistry.registerComponent('weightTraining', () => App);
    }

完整代码(子组件)

import React from 'react'; 
import { Right, Body, Thumbnail, Container, ListItem, Text, Icon } from 'native-base';
import { AppRegistry
} from 'react-native';
import Headerc from './headerc';
import ExerciseList from './exerciseList';
import Button from './Button';

const ExerciseDetail = ({ exercise }) => {
  const { menu_name, menu_icon, parent_id, id } = exercise;
function NumberDescriber() {
      let description;
      if (menu_icon === 'noimg.jpg') {
        description = `http://www.xxxxxx.com/uploads/icons/${menu_icon}`;
      } else if (menu_icon === 'noimg.jpg') {
        description = menu_icon;
      } else {
        description = `http://www.xxxxx.com/uploads/icons/${menu_icon}`;
      }
  return description;
}
function btnPress(parent_id, id) {
   const App = () => (
     <Container>
       <Headerc headerText={'Fitness sdaf'} />
       <ExerciseList pId={parent_id} mId={id} />
     </Container>
   );
     console.log('-------------------------------');
       console.log(id); 
       console.log('+++++++++++++++++++++++++++');
    AppRegistry.registerComponent('weightTraining', () => App);
}
return (
  <ListItem>
    <Thumbnail square size={80} source={{ uri: NumberDescriber() }} />
    <Body>
      <Text>{menu_name}</Text>
      <Text note> {menu_name} exercise lists</Text>
    </Body>
    <Right>
    <Button onPress={() => btnPress(parent_id, id)}>
        <Icon name="arrow-forward" />
    </Button>
    </Right>
  </ListItem>
  );
 }; 
export default ExerciseDetail;

如果您需要更多信息,请告诉我。

我不建议这样做,它看起来完全是反模式的,而不是。最好尝试导航或创建这样的模式

在您的索引中.js或 index.android.js 或 index.ios.js

import App from './App'    //your app level file
AppRegistry.registerComponent('weightTraining', () => App);

现在在您的应用程序 JS 文件中

export default App class extends React.Component{
 constructor(props){
   super(props);
   this.state ={
    component1:false,
    component2:true,  
  }
 }
btnPressed =()=>{
//handle state update logic here
}
render(){
  if(this.state.component1) return <Component1/>
  return <Component2/>
}
}

不是最好的解决方案,玩一玩,你会得到最好的

要从此组件导航到父组件,除非您想实现自己的导航(不推荐),否则您应该研究一个已经被 react-native 生态系统中的许多人构建和采用的导航。

一些最大的:

  • 反应原生导航
  • 反应导航
  • 反应原生路由器

我个人强烈推荐选项 1,因为它似乎是目前经过生产测试和生产就绪程度最高的实现

最新更新