Uncaught TypeError: Cannot Read Property 'firstChild' Of Undefined In React, React-Bootstrap
My code was fine until I tried demoing a ReactBootstrap Modal. When I initialized the show
prop passed to my Modal component from my top-level component to true (this.state.showResult
in QuizApp
), I keep gettingUncaught TypeError: Cannot read property 'firstChild' of undefined
When showResult
is initialized to false, it works fine.
The error occurs whenever show
prop of Modal is set to true.
Eventually I'll have event handlers that set showResult
to true, but for now I just wanted to see what the Modal would look like, thus initializing showResult: true
.
The only relevant information I could find was that this error is caused by multiple versions of React on the same page, but I don't think that applies here. I'm creating a really simple app, doing all my transpiling/building in the browser itself, no pre-building with node
TypeError when using React: Cannot read property 'firstChild' of undefined
What makes the situation even more confusing is that show
is a newly added prop that shows up in the code examples on ReactBootstrap docs but I couldn't find anything written about it in the docs.
How to open/close react-bootstrap modal programmatically?
class QuizApp
class QuizApp extends React.Component {
constructor(props) {
super(props);
this.state = { quiz: {questions: []}, showResult: true };
}
componentDidMount() {
var quizRef = new Firebase('https://firequiz.firebaseio.com/quizzes/' + this.props.i);
quizRef.on('value', snapshot => this.setState({
quiz: snapshot.val()
}));
}
render() {
let closeResult = e => this.setState({ showResult: false });
return (
<div>
{quizHeader}
<QuestionList data={this.state.quiz.questions}/>
<Result show={this.state.showResult} onHide={closeResult} />
</div>
);
}
}
class Result
class Result extends React.Component {
render() {
return (
<Modal {...this.props} bsSize='medium' aria-labelledby='contained-modal-title-md'>
<Modal.Header closeButton>
<Modal.Title id='contained-modal-title-md'>Your score</Modal.Title>
</Modal.Header>
<Modal.Body>
hello
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
}
Answer
It was indeed a conflict with two different react
versions. The react-bootstrap
build I was using was bundled with react 0.14
, while the react
I was using was 0.13.3
Using v0.24.5 build of react-bootstrap
instead, fixed the issue.
https://github.com/react-bootstrap/react-bootstrap/issues/1179
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM