I'm trying server-side rendering for the first time in my React/Redux app. An issue I'm having right now is that I need the initial state to have a randomly generated string and then pass it as a prop to my main App
component. This is obviously causing an issue because it's generating different strings for the client and server. Is there something I can do to stop this issue from happening?
Basic structure to help with understanding:
App.js
import React from 'react';
import { connect } from 'react-redux';
const App = ({ randomStr }) => (
<div>
<p>{randomStr}</p>
</div>
);
const mapStateToProps = (state) => ({
...
});
const mapDispatchToProp = (dispatch) => ({
...
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);
And my reducer:
reducer.js
import { generateString } from '../util';
import { NEW_STRING } from '../constants';
const stringReducer = (state = generateString(), action) => {
switch (action.type) {
case NEW_STRING:
return generateString();
default:
return state;
}
};
export default stringReducer;
As you can see, I'm getting the randomStr
from my redux store and rendering it, but it is different in client and server. Any help would be appreciated!
generateString
that takes no arguments and returns a string is either a constant or impure. – LocustgenerateString()
inside the reducer anymore, i do it as the initial state when I create the store. However, I still have the issue that it's generating differently for the client and server, so that when it is passed down as a prop it is causing issues. Is there any way around this? – Replicate