在s时更改子组件的prop

时间:2018-11-12 15:02:22

标签: javascript reactjs graphql gatsby

我正在使用Gatsby构建我的网站,并且试图在用户单击课程标题时更改Plyr组件的URL属性。现在,URL已在this.state中设置,数据已从GraphQL查询拉至Contentful。

以下是实时版本:https://brave-jepsen-d8ae28.netlify.com/courses/my-first-course

我创建了一个handleChange函数以使用event.target.id设置状态,但是一旦我单击课程段落标签,它就会返回错误。知道发生了什么吗?我测试了事件ID是用console.log(event.target.id)捕获的,并且确实如此,因此我假设尝试设置状态后再次渲染该组件。

感谢您的帮助!

import React, { Component } from 'react'
import { Link, graphql } from 'gatsby'
import Layout from '../components/layout' 
import Plyr from 'react-plyr'
import '../components/plyr.css'

// On click, set the value of that elements video url to video player url value

class CourseTemplate extends Component {
    constructor(props) {
        super(props);
        this.state = {
            url: props.data.contentfulCourse.featureVideo
        };
        this.handleChange = this.handleChange.bind(this);
    };

    handleChange(event) {
        event.stopPropagation();
        // console.log(event.target.id)
        this.setState(prevState => ({
             url: event.target.id
        }));
      }

    render() {
        const courseContent = this.props.data.contentfulCourse
    return (
        <Layout>
            <Plyr key={this.state.url} type="vimeo" videoId={this.state.url}/>
            <p>{this.state.url}</p>
            <h1>{courseContent.title}</h1>
            <p>{courseContent.shortDescription}</p>
            <div>
                {courseContent.teachers.map(teacher => (
                    <div key={teacher.id}>
                        <p>by <Link to={`/teachers/${teacher.id}`}>{teacher.name}</Link></p>
                    </div>
                ))}
            </div>
            <div>
                {courseContent.lessons.map(lesson => (
                    <div key={lesson.lessonVideo}>
                        <p onClick={this.handleChange} id={lesson.lessonVideo}>{lesson.title}</p>
                    </div>
                ))}
            </div>
        </Layout>   
    )
    }
};

export default CourseTemplate

export const query = graphql`
  query CourseTemplate($id: String!) {
    contentfulCourse(id: {eq: $id}) {
      title
      shortDescription
      featureVideo
      slug
      teachers {
        id
        name
      }
      lessons {
        id
        title
        lessonVideo
        slug
      }
    } 
  }
`

我收到的错误:

Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property 'target' on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist().

TypeError: Cannot read property 'id' of null at ProxyComponent.eval

1 个答案:

答案 0 :(得分:2)

您完全不需要使用event。您也不需要将函数传递给setState,因为您没有对prevState做任何事情。

import React, { Component } from "react"
import { Link, graphql } from "gatsby"
import Layout from "../components/layout"
import Plyr from "react-plyr"
import "../components/plyr.css"

class CourseTemplate extends Component {
  constructor(props) {
    super(props)
    this.state = {
      url: props.data.course.featureVideo,
    }
    this.setVideoUrl = this.setVideoUrl.bind(this)
  }

  setVideoUrl(url) {
    this.setState({ url })
  }

  render() {
    const courseContent = this.props.data.course

    return (
      <Layout>
        <Plyr type="vimeo" videoId={this.state.url} />

        <div>
          {courseContent.lessons.map(lesson => (
            <div key={lesson.id}>
              <p onClick={() => this.setVideoUrl(lesson.videoUrl)}>
                {lesson.title}
              </p>
            </div>
          ))}
        </div>
      </Layout>
    )
  }
}

export default CourseTemplate

export const query = graphql`
  query CourseTemplate($id: String!) {
    course: contentfulCourse(id: { eq: $id }) {
      title
      shortDescription
      featureVideo
      slug
      teachers {
        id
        name
      }
      lessons {
        id
        title
        videoUrl: lessonVideo
        slug
      }
    }
  }
`