I am using rails 5.1.7 with the react-rails gem, having a react component in one of my erb views with the react_component() helper I am trying to make an onChange event work, but the function does not respond in any way. I should be able to see the console log, but I am not seeing it.
import React from "react"
import PropTypes from "prop-types"
class CardList extends React.Component {
constructor(props) {
super(props);
this.state = {
all_cards: this.props.all_cards,
orderBy: 'all',
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
console.log('change was made');
}
render () {
const allData = this.state.all_cards.map((card, index) => (
<div className="col-md-12">
{ card.category }
</div>
));
return (
<React.Fragment>
<label for="order">Order</label>
<select onChange={this.handleChange} name="order" id="order_filter">
<option value="element1">element1</option>
<option value="element2">element2</option>
</select>
{allData}
</React.Fragment>
);
}
}
CardList.propTypes = {
all_cards: PropTypes.array
};
export default CardList
The component is called in my view as:
<%= react_component("cardlist", { all_cards: @all_cards }, {prerender: true}) %>
If I try adding the following to the serverRendering.js file:
ReactRailsUJS.mountComponents()
I get this error in the page the component is supposed to mount:
ExecJS::ProgramError in Cards#index
ReferenceError: document is not defined
I have also tried replacing React.Fragment
with a <div>
tag or changing the state in handleChange