2016-06-15 122 views
0

我使用的是redux wizard forms,我有一个名为HEADER的组件,它根据状态的页码改变文本。我可以改变头在每个页面的title属性,但我想改变字幕属性,所以我可以做这样的事情:React未捕获的参考错误

subtitle={this.state.subtitle } 

我想我可以打电话给一个叫updateComponents(功能),做一个switch语句看什么字幕应该更新,但下面的代码给了我这样的:

ReferenceError: updateComponents is not defined. 

我不知道我做错了,或者如果这是更新状态的最好方法。

import React, { Component, PropTypes } from 'react' 
import Step1Page from './step1Page' 
import Step2Page from './step2Page' 
import Step3Page from './step3Page' 
import Header from '../Template/header'; 

class NewSign extends Component { 
    constructor(props) { 
    super(props) 

    this.nextPage = this.nextPage.bind(this) 
    this.previousPage = this.previousPage.bind(this) 
    this.updateComponents = this.updateComponents.bind(this) 
    this.state = { 
     page: 1, 
     subtitle:"Basic Info" 
    } 
    } 

    updateComponents(){ 
    console.log(this.state.page); 
    } 


    nextPage() { 
    this.setState({ page: this.state.page + 1 }) 
    updateComponents(); 
    } 

    previousPage() { 
    this.setState({ page: this.state.page - 1 }) 
    updateComponents(); 
    } 


    render() { 
    const { onSubmit } = this.props 
    const { page } = this.state 
    return (
     <div className="container"> 
      <Header 
      title={"Step " + this.state.page } 
      subtitle="Basic Info" /> 

      {page === 1 && <Step1Page onSubmit={this.nextPage}/>} 
      {page === 2 && <Step2Page previousPage={this.previousPage} onSubmit={this.nextPage}/>} 
      {page === 3 && <Step3Page previousPage={this.previousPage} onSubmit={this.nextPage}/>} 
     </div> 

    ) 
    } 
} 

NewCampaign.propTypes = { 
    onSubmit: PropTypes.func.isRequired 
} 

export default NewSign 
+1

尝试并将previousPage()和nextPage()中的updateComponents()更改为this.updateComponents()。我认为它应该工作 –

+0

谢谢,这工作! – lost9123193

回答

1

编辑您的代码。您必须使用this声明调用函数。

import React, { Component, PropTypes } from 'react' 
import Step1Page from './step1Page' 
import Step2Page from './step2Page' 
import Step3Page from './step3Page' 
import Header from '../Template/header'; 

class NewSign extends Component { 
    constructor(props) { 
    super(props) 

    this.nextPage = this.nextPage.bind(this) 
    this.previousPage = this.previousPage.bind(this) 
    this.updateComponents = this.updateComponents.bind(this) 
    this.state = { 
     page: 1, 
     subtitle:"Basic Info" 
    } 
    } 

    updateComponents(){ 
    console.log(this.state.page); 
    } 


    nextPage() { 
    this.setState({ page: this.state.page + 1 }) 
    this.updateComponents(); 
    } 

    previousPage() { 
    this.setState({ page: this.state.page - 1 }) 
    this.updateComponents(); 
    } 


    render() { 
    const { onSubmit } = this.props 
    const { page } = this.state 
    return (
     <div className="container"> 
      <Header 
      title={"Step " + this.state.page } 
      subtitle="Basic Info" /> 

      {page === 1 && <Step1Page onSubmit={this.nextPage}/>} 
      {page === 2 && <Step2Page previousPage={this.previousPage} onSubmit={this.nextPage}/>} 
      {page === 3 && <Step3Page previousPage={this.previousPage} onSubmit={this.nextPage}/>} 
     </div> 

    ) 
    } 
} 

NewCampaign.propTypes = { 
    onSubmit: PropTypes.func.isRequired 
} 

export default NewSign 
相关问题