简体   繁体   English

无法在react-native上下文api中读取未定义的属性“状态”?

[英]Cannot read property 'state' of undefined in react-native context api?

I'm trying to context api in my ract-native app. 我正在尝试在我的应用程序中使用上下文API。 But i'm getting this error. 但我收到此错误。 TypeError: TypeError: Cannot read property 'number' of undefined. TypeError:TypeError:无法读取未定义的属性“ number”。 What is wrong my code? 我的代码有什么问题?

appContext.js appContext.js

import React from 'react';

export const AppContext = React.createContext();

class AppProvider extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            number: 10,
        };
    }

    render() {
        return 
        (
            <AppContext.Provider value={this.state}>
                {this.props.children}
            </AppContext.Provider>
        )
    }
}

export default AppProvider;

homeScreen.js homeScreen.js

import { AppContext } from './appContext';
<AppContext.Consumer>
    {(context) => context.number}
</AppContext.Consumer>

This is because you are not using your AppProvider component anywhere in your App. 这是因为您没有在App的任何地方使用AppProvider组件。 Try like this: 尝试这样:

const AppContext = React.createContext();

class AppProvider extends React.Component {
  constructor( props ) {
    super( props );
    this.state = {
      number: 10,
    };
  }

  render() {
    return (
      <AppContext.Provider value={this.state}>
        {this.props.children}
      </AppContext.Provider>
    );
  }
}

class App extends React.Component {
  render() {
    return (
      <AppProvider>
        <AppContext.Consumer>
          {context => context.number}
        </AppContext.Consumer>
      </AppProvider>
    );
  }
}

AppProvider is merely a standard component here. AppProvider在这里仅仅是一个标准组件。 It renders the context's Provider and that one gets some children. 它呈现了上下文的Provider并且Provider一些孩子。 So, you should use this AppProvider component somewhere in your app and pass a child with a Consumer . 因此,您应该在应用程序中的某个位置使用此AppProvider组件,并将带有Consumer的子对象传递给它。

If you want to keep your context in a separate file it would be like this: 如果要将上下文保存在单独的文件中,将如下所示:

Context and provider component 上下文和提供者组件

import React from "react";

export const AppContext = React.createContext();

class AppProvider extends React.Component {
  constructor( props ) {
    super( props );
    this.state = {
      number: 1745,
    };
  }

  render() {
    return (
      <AppContext.Provider value={this.state}>
        {this.props.children}
      </AppContext.Provider>
    );
  }
}

export default AppProvider;

Main component 主要成分

import React from "react";
import AppProvider, { AppContext } from "./AppProvider";

class App extends React.Component {
  render() {
    return (
      <AppProvider>
        <AppContext.Consumer>
          {context => context.number}
        </AppContext.Consumer>
      </AppProvider>
    );
  }
}

export default App;

暂无
暂无

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

相关问题 如何解决无法读取react-native上未定义的属性“ push”? - how to solve Cannot read property 'push' of undefined on react-native? React-Native Navigator:无法读取未定义的属性“ push” - React-Native Navigator: Cannot read property 'push' of undefined "React-Native AsyncStorage:TypeError:无法读取未定义的属性“setItem”" - React-Native AsyncStorage: TypeError: Cannot read property 'setItem' of undefined react-native - 类型错误:无法读取未定义的属性“修剪” - react-native - TypeError: Cannot read property 'trim' of undefined React-native:无法读取未定义的属性“ navigate” - React-native:cannot read property 'navigate' of undefined 'TypeError:无法读取 react-native 中未定义'的属性'getStateForAction' - 'TypeError: Cannot read property 'getStateForAction' of undefined ' in react-native 无法读取未定义 React-Native Firebase React-native-fetch-blob 的属性“DocumentDir” - Cannot read property 'DocumentDir' of undefined React-Native Firebase React-native-fetch-blob React无法通过API调用读取未定义的属性状态 - React Cannot read property state of undefined with API call react-native redux登录表单抛出&#39;TypeError:无法读取未定义的属性&#39;displayName&#39;&#39;&#39; - react-native redux Login Form throws an 'TypeError: Cannot read property 'displayName' of undefined'' 尝试执行本机应用程序时出现“错误无法读取未定义的属性‘拆分’” - "error Cannot read property 'split' of undefined" in trying to execute react-native app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM