React Uncaught ReferenceError:variable is not defined

问题描述:

我使用一个组件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

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

+0

我不认为这是问题所在。它可能是一个不同的模块,再加上这是组件 – Li357

+0

hm内的一个'ReferenceError',够公平的。让我们看看他说了什么,如果猜测不正确,我会删除我的答案。 – lustoykov

+0

等一下,你实际上可能是对的,因为OP说他们是第二次在AddCarWashPage里面调用它。如果是这种情况,我会说,将其标记为错字。 – Li357