2016-09-06 249 views
0

我使用一個組件2次。當它第一次呼叫時是好的,但當我第二次呼叫時(通過反應路由器移動到不同的組件),我有例外Uncaught ReferenceError: titleStyle is not defined。在控制檯中,我看到這一行中的問題:_react2.default.createElement("h2", { style: titleStyle },this.props.title,":") 我做錯了什麼?React Uncaught ReferenceError:variable is not defined

TitleWithAddButton.jsx(有問題的組分)

import React from 'react'; 
import {Link} from 'react-router' 

export default class TitleWithAddButton extends React.Component{ 
render(){ 
    let titleStyle = { 
     width:"50%" 
    }; 

    var button = { 
     width: "10%", 
     float: "right" 
    }; 

    return (
     <div className="title-with-add-button"> 
      <div> 
       <Link to="/carwashAdd"><button type="button" className="btn btn-success" style={button}>Add</button></Link> 
      </div> 
      <h2 style={titleStyle}>{this.props.title}:</h2> 
     </div> 
    ) 
} 
} 

CarWashPage.jsx(第一次componen是從它的呼叫)

import React from 'react'; 
import TitleWithAddButton from './TitleWithAddButton.jsx'; 
import AllCarWashTable from './carwash/AllCarWashTable.jsx' 

export default class CarWashPage extends React.Component{ 

render(){ 
    var carWashPageStyle = { 
     paddingLeft: 10, 
     paddingRight: 10 
    } 

    return (
     <div style={carWashPageStyle}> 
      <TitleWithAddButton title="All carwash"/> 
      <AllCarWashTable/> 
     </div>  
    ) 
} 
} 

AddCarWashPage.jsx(第二次組件是從這裏撥打的)

import React from 'react'; 
import Title from './../Title.jsx' 

export default class AddCarWashPage extends React.Component{ 

render(){ 
    var addCarWashPage = { 
     paddingLeft: 10, 
     paddingRight: 10 
    } 

    return (
     <div style={addCarWashPage}> 
      <Title title="Add CarWash"/> 
     </div> 
    ) 
} 
} 
+0

你可以爲你的'Title.jsx'組件添加代碼嗎? – lustoykov

回答

1

您的意思是包含TitleWithAddButton的文件AddCarWashPage.jsx您的第二次通話發生,但包括import Title from './../Title.jsx'

+0

我不認爲這是問題所在。它可能是一個不同的模塊,再加上這是組件 – Li357

+0

hm內的一個'ReferenceError',夠公平的。讓我們看看他說了什麼,如果猜測不正確,我會刪除我的答案。 – lustoykov

+0

等一下,你實際上可能是對的,因爲OP說他們是第二次在AddCarWashPage裏面調用它。如果是這種情況,我會說,將其標記爲錯字。 – Li357

相關問題