首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Context - "this“未定义

React Context - "this“未定义
EN

Stack Overflow用户
提问于 2020-03-20 02:28:23
回答 1查看 157关注 0票数 1

我使用React上下文来管理全局状态。

因此,我已经通过它的提供者和消费者定义了我的上下文。

我有我的视频播放-context.js

代码语言:javascript
复制
import React from "react";
import { createContext } from 'react';

// set the defaults
const VideoContext = React.createContext({
  videoPlaying: false,
  setPlayingVideo: () => {}
});

export default VideoContext;

在我的_app.js中,我有:

代码语言:javascript
复制
import App from 'next/app'
import { PageTransition } from 'next-page-transitions'
import VideoContext from '../components/videoplaying-context'

class MyApp extends App {
  setPlayingVideo = videoPlaying => {
    this.setState({ videoPlaying });
  };

  state = {
    videoPlaying: false,
    setPlayingVideo: this.setPlayingVideo
  }
  render() {
    console.log('new _app.js defalt page');
    const { Component, pageProps, router, state } = this.props
    return (
      <React.Fragment>
        <VideoContext.Provider value={this.state}>
          <PageTransition timeout={300} classNames="page-transition">
            <Component {...pageProps} key={router.route} />
          </PageTransition>
        </VideoContext.Provider>
      </React.Fragment>
    )
  }
}

export default MyApp

然后在我的一个文件中,我放入了消费者:

代码语言:javascript
复制
import Layout from "../components/Layout";
import ReactPlayer from 'react-player
import VideoContext from '../components/videoplaying-context'

class Video extends React.Component {
  constructor(props) {
    super(props);
    this.triggerVideo = this.triggerVideo.bind(this);
  }
  triggerVideo(event) {
    console.log("click");
    /* doing other stuff here... */
  }
  render() {
    return (
      <VideoContext.Consumer>
        {context => (
          <Layout>
            <h1>Videos</h1>
            <div>
              <div className="group" id="process-video">
                <div
                  className="poster-image"
                  onClick={() => {
                    this.triggerVideo.bind(this);
                    context.setPlayingVideo(true);
                  }}
                />
                <ReactPlayer
                  url="https://vimeo.com/169599296"
                  width="640px"
                  height="640px"
                  config={{
                    vimeo: {
                      playerOptions: {
                        thumbnail_url: "http://placehold.it/640x640.jpg",
                        thumbnail_width: 640,
                        thumbnail_height: 640
                      }
                    }
                  }}
                />
              </div>
            </div>
            <style jsx global>{`
              .group {
                position: relative;
                height: 0;
                overflow: hidden;
                height: 640px;
                width: 640px;
              }

              .poster-image {
                background: url("http://placehold.it/640x640.jpg") center center;
                background-size: cover;
                bottom: 0;
                left: 0;
                opacity: 1;
                position: absolute;
                right: 0;
                top: 0;
                z-index: 10;
                height: 640px;
                width: 640px;
                transition: all 0.4s ease-in;
              }

              .poster-image + div {
                position: absolute;
                top: 0;
                left: 0;
                width: 640px;
                height: 640px;
              }

              .poster-image.video--fadeout {
                opacity: 0;
              }
            `}</style>
          </Layout>
        )}
      </VideoContext.Consumer>
    );
  }
}

export default Video;

因此,函数“context.setPlayingVideo(VideoPlaying)”运行良好,并且正确地将全局状态"this“设置为true,但是,在引入上下文之后,”this.triggerVideo.bind(context.setPlayingVideo);“不再起作用,因为”this“是未定义的。

我试着移除它和其他东西,但我真的卡住了,我不知道如何修复它。

谢谢大家!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-20 04:14:34

在此行中,您不会调用方法triggerVideo

代码语言:javascript
复制
onClick={() => { this.triggerVideo.bind(this); context.setPlayingVideo(true); }}

更改为:

代码语言:javascript
复制
onClick={() => { this.triggerVideo(); context.setPlayingVideo(true); }}

或发送到:

代码语言:javascript
复制
onClick={() => { this.triggerVideo.bind(this)(); context.setPlayingVideo(true); }}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60763090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档