-
Notifications
You must be signed in to change notification settings - Fork 755
Using Redux Provider: id mismatch? #878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm also rendering react components with a redux Provider on the server with rails 5.1.4 and ruby 2.5.0, but it doesn't reproduce. |
When I have time I'll try add a redux example branch to react-rails-example-apps. |
+1. Although Redux has nothing to do with this gem, many users of this gem seems to be struggling to use Redux with this gem. FYI. My usage example of Redux with this gem is below: # app/controllers/posts_controller.rb
class PostsController < ApplicationController
def show
render component: 'Post', props: { post: { id: params[:id], body: 'foo bar' } }, prerender: true
end
end // app/javascript/components/Post.js
import React from "react"
import { Provider, connect } from 'react-redux';
import { createStore, combineReducers } from 'redux';
function post(state = null, action) {
return state;
}
const reducer = combineReducers({ post });
function PostComponent({ id, body }) {
return (
<div>
<h1>Post</h1>
<div>{id}</div>
<div>{body}</div>
</div>
);
}
function mapStateToProps(state) {
return state.post;
}
const PostContainer = connect(mapStateToProps)(PostComponent);
export default function Post(props) {
const store = createStore(reducer, props);
return (
<Provider store={store}>
<PostContainer />
</Provider>
);
} $ curl localhost:3000/posts/42
<!DOCTYPE html>
<html>
<head>
<title>ReactRailsWithReduxExample</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="rdTZeqYi1qXrPofTTl/7yEGe9SnRe2zInWSPbuk2nrU/WmcKLaMZmRImvMfw3L2+lSpWwI8fEWzy2PvuShcAEA==" />
<script src="/packs/application-ad21c81663060032e2ac.js"></script>
</head>
<body>
<div data-react-class="Post" data-react-props="{"post":{"id":"42","body":"foo bar"}}" data-hydrate="t"><div data-reactroot=""><h1>Post</h1><div>42</div><div>foo bar</div></div></div>
</body>
</html> repo: https://github.com/ttanimichi/react_rails_with_redux_example |
<3 @ttanimichi thank you for that example! There is probably a real issue where |
@BookOfGreg You're so kind. Don't get burnt out over this 🙂 |
thank you @BookOfGreg & @ttanimichi for the all the responses This was actually due to user error. We were generating unique id's somehow in the components, causing a mismatch. Here's a question though: https://redux.js.org/recipes/server-rendering Based on what redux says on their official site, you always want to use the exact store generated from the server side, on the client. We were able to do this by doing the following:
Let me know what you think? |
@reywright
How do you do that? I tried Here's my code:
app/javascript/packs/server_rendering.js
Currently, the Redux store is rendered server-side and the list of companies is also rendered server-side (taking the data out of store). But I can't transfer the store to the client for the moment. Any ideas? |
@ttanimichi |
I don’t understand what you mean. Hydration is a matter of React’s SSR and it’s not related to Redux stroe. You can pass props to a component by using |
Did you try this example? #878 (comment) |
By 'hydration' I meant this: https://redux.js.org/recipes/serverrendering#client-js
Surprisingly, looks like you're right. I just tried to do it myself - it works - I don't understand how - but it works. But I made it all a bit easier. I do not create a Here's my code (that's not an SPA, so react-router is not involved, I'll try react-router in future): app/views/companies/index.html.erb app/javascript/components/ProviderIndexContainer.js
app/javascript/components/CompanyListContainer.js
app/javascript/components/CompanyList.js
app/javascript/redux/store/index.js
app/javascript/redux/store/initial-state.js
It looks like that gem To check it practically I modified app/javascript/components/CompanyListContainer.js:
and I saw the Redux store client-side, in the console (when pressed the 'Refresh companies list' button). How does And switching from rendering the Redux
to
and the rendered companies list respectively disappears from the fetched HTML. PS One thing that bothers me is whether I (and @ttanimichi) do it all correctly. Because what I portrayed here is so much far away from the https://redux.js.org/recipes/serverrendering picture. But one thing is clear - I hate the https://github.com/shakacode/react_on_rails gem because it looks so ugly, ponderous, poorly documented and even partially commercial (!). I am fed up with their ads like "hire us" on the docs page. :) That gem has to be punished by no-usage. |
As I already told you, the hydration in the example has nothing to do with Redux.
Congrats.
If you specify react-rails/react_ujs/index.js Line 98 in f02a3af
react-rails 2.4.1+ supports React's hydration. ref. #828
No, it doesn't.
Yes.
Without the option |
If you have any confirming references to this statement, it would be nice to see them. Not necessarily right now, but maybe someday later. I hope everybody would be interested to see it.
I just mean that it is easy to globally change a website's SSR presence - in a single place:
Looks like that by default it is nice to enable SSR website-wide (if this is as easy as to change PS My next step - to try to use react-router + SSR + Redux for this gem. |
Going through the above discussion, the issue can be closed. |
Steps to reproduce
Render any react component with a Provider on the server (set prerender as true)
Expected behavior
The component should be generated from the server, then UJS should mount the client version
Actual behavior
System configuration
Sprockets or Webpacker version: No version (Latest)
React-Rails version: No Version (Latest)
Rect_UJS version: React-Rails specified.
Rails version: 5.1.4
Ruby version:2.5.0
I'm attempting to pre-render a component with a Provider, but it has issues when UJS tries to automatically mount the client version on top of it.
react-on-rails
is not only too far of a departure, but it's too battery included, no migration instructions, and I'd like to depend on this more official release.The text was updated successfully, but these errors were encountered: