React:上下文在组件的两个层次结构之间传递状态

时间:2019-06-23 13:16:19

标签: javascript reactjs state react-context

我正在开发一个网站,希望在该网站中访问应用程序中任何位置的状态信息。我尝试了几种实现状态的方法,但始终收到以下错误消息:

元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。

检查SOS的呈现方法。

这是我的SOS-> index.js文件:

import React, { useContext } from 'react';
import axios from 'axios';
import CONST from '../utils/Constants';
import { Grid, Box, Container } from '@material-ui/core';
import { styled } from '@material-ui/styles';
import { Header } from '../Layout';
import ListItem from './ListItem';
import SOSButton from './SOSButton';
import FormPersonType from './FormPersonType';
import FormEmergencyType from './FormEmergencyType';
import StateContext from '../App';
import Context from '../Context';

export default function SOS() {
  const { componentType, setComponentType } = useContext(Context);
  const timerOn = false;
  //'type_of_person',
  const ambulance = false;
  const fire_service = false;
  const police = false;
  const car_service = false;

  //static contextType = StateContext;
  const showSettings = event => {
    event.preventDefault();
  };

  const handleComponentType = e => {
    console.log(e);
    //this.setState({ componentType: 'type_of_emergency' });
    setComponentType('type_of_emergency');
  };

  const handleEmergencyType = new_emergency_state => {
    console.log(new_emergency_state);
    //   this.setState(new_emergency_state);
  };

  const onSubmit = e => {
    console.log('in OnSubmit');
    axios
      .post(CONST.URL + 'emergency/create', {
        id: 1,
        data: this.state //TODO
      })
      .then(res => {
        console.log(res);
        console.log(res.data);
      })
      .catch(err => {
        console.log(err);
      });
  };

  let component;

  if (componentType == 'type_of_person') {
    component = (
      <FormPersonType handleComponentType={this.handleComponentType} />
    );
  } else if (componentType == 'type_of_emergency') {
    component = (
      <FormEmergencyType
        handleComponentType={this.handleComponentType}
        handleEmergencyType={this.handleEmergencyType}
        emergencyTypes={this.state}
        timerStart={this.timerStart}
        onSubmit={this.onSubmit}
      />
    );
  }
  return (
    <React.Fragment>
      <Header title="Send out SOS" />
      <StateContext.Provider value="type_of_person" />
      <Container component="main" maxWidth="sm">
        {component}
      </Container>
      {/*component = (
        <HorizontalNonLinearStepWithError
          handleComponentType={this.handleComponentType}
        />*/}
    </React.Fragment>
  );
}

非常感谢您的帮助!

仅供参考,上下文文件定义如下:

import React, { useState } from 'react';

export const Context = React.createContext();

const ContextProvider = props => {
  const [componentType, setComponentType] = useState('');
  setComponentType = 'type_of_person';
  //const [storedNumber, setStoredNumber] = useState('');
  //const [functionType, setFunctionType] = useState('');
  return (
    <Context.Provider
      value={{
        componentType,
        setComponentType
      }}
    >
      {props.children}
    </Context.Provider>
  );
};

export default ContextProvider;

编辑:我已经根据您的建议更改了我的代码(以上更新)。但是现在我得到以下错误: TypeError:无法读取未定义的属性“ componentType”

1 个答案:

答案 0 :(得分:0)

Context不是../Context文件的默认导出,因此您必须将其导入为:

import { Context } from '../Context';

否则,它将尝试导入您的Context.Provider组件。

对于您的文件结构/命名,正确的用法是:

// Main app file (for example)
// Wraps your application in the context provider so you can access it anywhere in MyApp
import ContextProvider from '../Context'

export default () => {
  return (
    <ContextProvider>
      <MyApp />
    </ContextProvider>
  )
}
// File where you want to use the context
import React, { useContext } from 'react'
import { Context } from '../Context'

export default () => {
  const myCtx = useContext(Context)

  return (
    <div>
      Got this value - { myCtx.someValue } - from context
    </div>
  )
}

还有一件令人敬畏的事……将您的Context文件,提供程序以及其中的所有内容重命名为更明确的名称。我什至不敢写。