I am trying to build a simple application with Ruby on Rails and React. I am using a gem react_on_rails. In addition, I am trying to use Redux to communicate between different components when I need. The issue that I having at the current moment I don't understand how to hold Store in the application. If it a plain React app, I don't have a problem to share the Redux between components. In React On Rails is a different way to setup it and it works fine at current implementation, but I am losing the store when I switching to a different page or refreshing the page.
My goal is to add the product ID to the cart via Redux and them when user redirects to the /cart
page it can retrieve the cart array from Redux store and then I can populate the page with needed products.
Example of simple view component in Rails.
<div class='fancyClass'>
<%= react_component("ProductDetail", props: {product: @product.id}) %>
</div>
Example of simple react component which adding a product to the cart array
// @flow
import * as React from 'react';
import { Provider, connect } from 'react-redux';
import { setCart } from 'action/SetCart';
import { createStore } from 'redux';
import rootReducer from '../reducers';
const store = createStore(rootReducer);
type Props = {
product: number
}
export class Scratch1 extends React.Component<Props> {
onClick = () => {
this.props.setCart(this.props.product);
window.location.assign('/cart')
};
render() {
return (
<Provider store={store}>
<button onClick={this.onClick}>add to cart</button>
</Provider>
);
}
}
const Scratch1Redux = connect(
state => ({
cart: state.cart,
}),
dispatch => ({
setCart: id => {
dispatch(setCart(id))
}
})
)(Scratch1);
export default Scratch1Redux;
Thank you in advance