javascript

The following is the Android log output when the React Native app is started for a while before disconnecting it from the Meteor (DDP) server by killing the Meteor server.

Using console.log("<App /> render") to indicate when the render function of App component is called, it appears that whenever the render function is called again after createContainer passes it some new props , the Key is already defined error is triggered.

What's causing this error/warning and how can we fix it? The app still runs fine, but something is definitely wrong here.

12-23 02:27:01.875 31197 19338 I ReactNativeJS: Running application "RNapp" with appParams: {"initialProps":{},"rootTag"
:1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
12-23 02:27:01.891 31197 19338 I ReactNativeJS: render
12-23 02:27:01.995 31197 19338 I ReactNativeJS: Connected to DDP server.
12-23 02:27:01.999 31197 19338 I ReactNativeJS: Connected to DDP server.
12-23 02:27:02.012 31197 19338 I ReactNativeJS: render
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:02.013 31197 19338 I ReactNativeJS: Key root is already defined!
12-23 02:27:34.592 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:34.593 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:34.599 31197 19338 I ReactNativeJS: <App /> render
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:34.599 31197 19338 I ReactNativeJS: Key root is already defined!
12-23 02:27:34.609 31197 19338 I ReactNativeJS: <Loading /> render
12-23 02:27:35.603 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:35.613 31197 19338 I ReactNativeJS: <App /> render
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:35.613 31197 19338 I ReactNativeJS: Key root is already defined!
12-23 02:27:45.599 31197 19338 I ReactNativeJS: Disconnected from DDP server.
12-23 02:27:45.616 31197 19338 I ReactNativeJS: <App /> render
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key home is already defined!
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key welcome is already defined!
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key loading is already defined!
12-23 02:27:45.616 31197 19338 I ReactNativeJS: Key root is already defined!

index.android.js

import { AppRegistry } from 'react-native'

import App from './app/App.js'

AppRegistry.registerComponent('RNapp', () => App)

App Component

export class App extends Component {

    constructor(props) {
        super(props);
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.status.connected == false) {
            Actions.loading();
        } else {
            if (nextProps.user !== null) {
                Actions.home();
            } else {
                Actions.welcome();
            }
        }
    }


    render() {
       console.log('<App /> render')
        return (
            <Router>
                <Scene key="root">
                    <Scene key="home" component={Home} title="Home" hideNavBar={true} />
                    <Scene key="welcome" component={Welcome} title="Welcome" hideNavBar={true} />
                    <Scene key="loading" component={Loading} title="Loading" hideNavBar={true} />
                    <Scene key="profile" component={Profile} title="Home" hideNavBar={true} />
                    <Scene key="history" component={History} title="Home" hideNavBar={true} />
                    <Scene key="search" component={Search} title="Home" hideNavBar={true} />
                </Scene>
            </Router>
        )       
    }

}


export default createContainer(() => {
  return {
    status: Meteor.status(),
    user: Meteor.user(),
    loggingIn: Meteor.loggingIn(),
  };
}, App);

The warnings don't cause any harm, but you can get rid of them by defining your scenes in a variable and then including them as a prop to your router like so:

const scenes = Actions.create(
  <Scene key="root">
    <Scene key="home" component={Home} title="Home" hideNavBar={true} />
    <Scene key="welcome" component={Welcome} title="Welcome" hideNavBar={true} />
    <Scene key="loading" component={Loading} title="Loading" hideNavBar={true} />
    <Scene key="profile" component={Profile} title="Home" hideNavBar={true} />
    <Scene key="history" component={History} title="Home" hideNavBar={true} />
    <Scene key="search" component={Search} title="Home" hideNavBar={true} />
  </Scene>
)

export class App extends Component {

  constructor(props) {
    super(props);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.status.connected == false) {
      Actions.loading();
    } else {
      if (nextProps.user !== null) {
        Actions.home();
      } else {
        Actions.welcome();
      }
    }
  }


  render() {
    console.log('<App /> render')
    return (
      <Router scenes={scenes} />
    )
  }

}

See the docs for more info.


In my experience the warnings do not cause any problems, but just in case, I usually define a shouldComponentUpdate method that returns false if the props/state have not changed in a way that would require the route to change (note that I use redux and redux persist to decide what is the initial scene, and so I need to connect the component to redux in order to get the required information via props, but later if the data in the store changes, I can safely block additional renders).

That is just my approach based on my experience and needs. The router-flux guys have a slightly 'stricter' approach to this, that you can see in their docs

链接地址: http://www.djcxy.com/p/37794.html

上一篇: 将代码镜像应用于ng

下一篇: JavaScript的