简体   繁体   English

将React API上下文与React Router一起使用

[英]Using react api context alongside react router

I have a problem with passing context to route. 我在传递上下文路由时遇到问题。 I get an error when i click a link that goes to my component where context was passed from App component. 当我单击一个链接,该链接指向从应用程序组件传递上下文的组件时,我得到一个错误。 Below is that component with App (only one import just to show where Context is coming from): 以下是带有App的组件(仅一次导入即可显示Context的来源):

App.js App.js

import { Context } from './Context';
class App extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
       cryptolist: []
    }
  }

  componentDidMount = () => {
    fetch('https://api.coinmarketcap.com/v2/ticker/?structure=array')
      .then(response => response.json())
      .then(json => this.setState({
        cryptolist: json.data
      }))
  }

  render() {
    return (
      <div>
        <Menu />
        <Context.Provider value={this.state}>
          <Userlist />
        </Context.Provider>
      </div>
    )
  }
}

export default App;

Userlist.js ( should be cryptolist or something ) Userlist.js(应为cryptolist或其他名称)

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



export default class Userlist extends Component {
  render() {
    return (
      <main>
        <Context.Consumer>
          {(context) => context.cryptolist.map(el => {
              return (
                  <div>
                      <h2>{el.name}</h2>
                      <h5>{el.symbol}</h5>
                      <h3>{el.quotes.USD.price}</h3>
                  </div>
              )
          })}
        </Context.Consumer>
      </main>
    )
  }
}

Context.js Context.js

import React from 'react';

export const Context = React.createContext();

Everything works just fine here untill i wanted to make a menu that links to this component. 在这里一切正常,直到我想制作一个链接到该组件的菜单。

import React from "react";
import { slide as Slider } from 'react-burger-menu';
import { BrowserRouter as Router, Route, Link, Switch} from "react-router-dom";
import Main from './main';
import Userlist from './userlist';



export default class Menu extends React.Component {
  render () {
    return (
        <Router>
        <div className="bg-navy w-100 h-100">
          <Slider width={ 180 } isOpen={ false }>
            <Link className="menu-item" to="/main">Home</Link>
            <Link className="menu-item" to="/crypto">About</Link>
          </Slider>
             <Switch>
                <Route path="/main" component={Main} />
                <Route path="/crypto" component={Userlist} />
             </Switch>
          </div>
      </Router>
    );
  }
}

When i click a link to component Userlist i get an error thats cryptolist is not defined. 当我单击指向组件用户列表的链接时,出现错误,即未定义密码列表。 I get it that Userlist can't see a context after clicking link to it. 我知道用户列表单击链接后看不到上下文。 How to pass it correctly? 如何正确通过?

You are using the routes in the Menu component. 您正在使用“ Menu组件中的路线。 Is this really you want? 这真的是您想要的吗? Though, I don't know how this slide thingy works. 虽然,我不知道这张幻灯片是如何工作的。 Maybe this is the way you want to go. 也许这就是您要走的路。 I think your problem occurs because your Menu component is not wrapped by the provider. 我认为您的问题发生是因为提供程序没有包装您的Menu组件。 Try like this: 尝试这样:

<Context.Provider value={this.state}>
    <Menu />
    <Userlist />
</Context.Provider

Your Menu component will call Userlist but as it is out the Provider the context doesn't exist! 您的Menu组件将调用Userlist但由于不在Provider中,因此上下文不存在! Replace Userlist in Context.Provider by Menu and all will be fine. 更换UserlistContext.Provider通过Menu ,一切都会好起来的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM