简体   繁体   English

'Readonly<{}> & Readonly<{ children?: ReactNode; 类型上不存在属性“导航” }>

[英]Property 'navigation' does not exists on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>

I am completely new to react native.我对本地反应完全陌生。

I have the following components:我有以下组件:

Playlists:播放列表:

export default class Playlists extends Component {
  playlists = [
    ...
  ];

  render() {
    const {navigation} = this.props.navigation;

    return (
      <FlatList
        data={this.playlists}
        renderItem={({item}) => (
          <TouchableOpacity
            style={styles.item}
            onPress={() => navigation.navigate('PlaylistDetails', item)}>
            <Text style={styles.text}>{item.name}</Text>
          </TouchableOpacity>
        )}
      />
    );
  }
}

Details View of a Playlist:播放列表的详细信息:

export default class PlaylistDetails extends Component {
  render() {
    const {navigation} = this.props.navigation;
    var songs: Song[] = navigation.getParam('songs');

    return (
      <View>
        <Text>{navigation.getParam('name')}</Text>
        <FlatList
          data={songs}
          renderItem={({item: song}) => <Text>{song.title}</Text>}
        />
      </View>
    );
  }
}

And for navigation:对于导航:

const screens = {
  Playlists: {
    screen: Playlists,
  },
  PlaylistDetails: {
    screen: PlaylistDetails,
  },
};

const stack = createStackNavigator(screens);

export default createAppContainer(stack);

But at const { navigation } = this.props.navigation;但是在const { navigation } = this.props.navigation; I get an error Property 'navigation' does not exist on type ... .我收到错误Property 'navigation' does not exist on type ...

I have tried adding the following code:我尝试添加以下代码:

interface Props {
    navigation: any
}

and then adding this:然后添加这个:

export default class PlaylistDetails extends Component<Props> {
    ...
}

This removes the errors, but when I run the application, I get the following error: TypeError: undefined is not an object (evaluating 'navigation.navigate') .这消除了错误,但是当我运行应用程序时,我收到以下错误: TypeError: undefined is not an object (evaluating 'navigation.navigate')

I am totally new to react native and don't know how to solve that.我对原生反应完全陌生,不知道如何解决。 I hope somebody can help me.我希望有人可以帮助我。

Everything is fine, and you are defining the right interface.一切都很好,您正在定义正确的接口。

However, you should be spreading the props, rather than going 1 level deep and spreading the navigation property within the props object.但是,您应该传播道具,而不是深入 1 级并在道具对象内传播navigation属性。

This is how it should be like:它应该是这样的:

export default class PlaylistDetails extends Component<Props> {
  render() {

    // edit this: 
    const { navigation } = this.props;
    var songs: Song[] = navigation.getParam('songs');

    return (
      <View>
        <Text>{navigation.getParam('name')}</Text>
        <FlatList
          data={songs}
          renderItem={({item: song}) => <Text>{song.title}</Text>}
        />
      </View>
    );
  }
}

暂无
暂无

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

相关问题 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' 打字稿错误属性 x 在类型“只读”上不存在<Props> &amp; Readonly&lt;{ children?: ReactNode; }&gt;&#39; - Typescript error Property x does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>' 属性 &#39;google&#39; 不存在于类型 &#39;Readonly&lt;{ IGoogleMapTrackerProps }&gt; &amp; Readonly&lt;{ chirldren?: ReactNode; }&gt;&#39; - Property 'google' does not exist on type 'Readonly<{ IGoogleMapTrackerProps }> & Readonly<{ chirldren?: ReactNode; }>' React JS中类型&#39;Readonly &lt;{}&gt;&#39;上不存在属性&#39;currentTime&#39; - Property 'currentTime' does not exists on type 'Readonly<{}>' in react js "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' 类型“ Readonly &lt;{}&gt;”上不存在属性“ xxx” - Property 'xxx' does not exist on type 'Readonly<{}>' "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 类型“只读&lt;{}&gt;”上不存在属性“产品” - Property 'products' does not exist on type 'Readonly<{}>' 类型不可分配给类型 'IntrinsicAttributes & { children?: ReactNode; }'。 财产 - Type is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'. Property vue-apollo - 类型“只读”上不存在属性“值” <ref<readonly<any> &gt;&gt;' </ref<readonly<any> - vue-apollo - Property 'value' does not exist on type 'Readonly<Ref<Readonly<any>>>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM