React Native Router Flux和React Native Meteor初始场景渲染问题

问题描述 投票:0回答:1

嗨我需要帮助我创建了一个索引页面,检查用户是否登录并相应地重定向,数据通过createContainer传递给该组件一切正常但现在登录后我已打开路由器页面并从那里重定向全部组件正确路由,除了带有withTracker的组件我再次被推到默认初始屏幕

Walkthrough->

1.)登录屏幕

2.)显示Timesheets(路由器中的初始)

3.)单击Leaves-> Apply Leave

4.)尝试开放申请休假

5.)打开一秒钟然后返回初始页面(Timesheets)

index.js

`const myApp = (props) => {
  const { status, user, loggingIn } = props;
  if (status.connected === false || loggingIn) {
    // return <Loading />;
    return <RouterStack />
  } else if (user !== null) {
    // return <Home />
    return <RouterStack />
  }
  return <Login />
  };
  myApp.propTypes = {
  status: PropTypes.object,
  user: PropTypes.object,
  loggingIn: PropTypes.bool,
  };
  export default createContainer(() => {
  return {
    status: Meteor.status(),
    user: Meteor.user(),
    loggingIn: Meteor.loggingIn(),
  };
  }, myApp);
`

Router.js

`
export default class App extends Component {
  constructor(props) {
    super(props)
  }
  render() {
    return (
      <Router getSceneStyle={() => styles.sceneStyle}>
        <Scene key="root">
          <Scene key="Timesheets" component={Timesheets} title="Timesheets" initial />
          <Scene key="applyLeave" component={ApplyLeave} title="Apply Leave"  />
          <Scene key="Login" component={Login} title="Login" />
          <Scene key="Leaves" component={Leaves} title="Leave" />
          <Scene key="ViewLeave" component={LeaveHistory} title="Leaves History"/>
          <Scene key="Profile" component={Profile} title="Profile" />
        </Scene>
      </Router>
    )
  }
}
`

ApplyLeave.js

`class ApplyLeave extends Component {
render(){
return <View><Text></Text></View> //just for Demo
}
}
export default withTracker(params => {
    if (Meteor.user() != null) {
        const employeeId = Meteor.user();
        const leaves = Meteor.subscribe('leaves', employeeId);
        const entitlements=Meteor.subscribe('entitlements',employeeId);      
        return {
            myLeavesReady: leaves.ready(),
            myEntitlements: entitlements.ready()
        };
    }
    return {}
})(ApplyLeave);
`
react-native react-native-router-flux react-native-meteor
1个回答
1
投票

你面临的问题实际上是一个功能,并表明一切正常(createContainer + React)。

发生的事情是你在createContainermyApp的return语句中的一个参数发生了变化(或者至少是被动地重新发送),并且因为myApp没有检查它是否应该重新渲染(shouldComponentUpdate)它默认情况下这样做=>你'重新回到初始屏幕。

执行以解决问题的步骤

现在,这取决于您是否需要在createContainermyApp中返回的数据。如果您需要数据或需要来自Meteor.user()的其他属性(例如可能更改的配置文件信息),您将需要创建自己的shouldComponentUpdate并检查这些值是否更新。

我不认为你的情况就是如此,所以你可能会很好地将你传递的道具改为你真正需要的最小值(不传递用户对象 - 注意Meteor.status()也是一个对象)并使用PureComponent以防止不必要的重新渲染。

class myApp extends React.PureComponent {

    render() {
        const { isConnected, isSignedIn, isSigningIn } = this.props;
        if (!isConnected || isSigningIn) {
            return <RouterStack />;
        } else if (isSignedIn) {
            return <RouterStack />;
        }
        return <Login />;
    }
}

myApp.propTypes = {
    isConnected: PropTypes.bool,
    isSignedIn: PropTypes.bool,
    isSigningIn: PropTypes.bool,
};

// if Meteor.something() is null, !!Meteor.something() is false
// if Meteor.something() is a valid object, !!Meteor.something() is true
export default createContainer(() => ({
    isConnected: !!Meteor.status().connected,
    isSignedIn: !!Meteor.user(),
    isSigningIn: !!Meteor.loggingIn(),  
}), myApp);
© www.soinside.com 2019 - 2024. All rights reserved.