<App foo="123" />
@connect((state) => state, () => {})
class App extends Component
And I'd like to render App with 123.
But, if state in MapStateToProps has a foo
key and its value is abc
, the component will render abc
.
I could check ownProps.
@connect((state, ownProps) => ({...state, ...ownProps}), () => {})
class App extends Component
and merge ownProps and state. But If I start dispatching actions to update foo in Redux, state will always be abc
. ownProps will always override keys in state.
I could dispatch an action when the component mounts.
componentDidMount() {
dispatchFoo(this.props.value)
}
when component mounts, I'm dispatching the value
@connect((state) => state, () => {})`
Store will be updated with abc
, the value of the own props.
Redux will update and the component will render once more.
But this time, state will be abc
in ..
@connect((state) => state, () => {})
What is the best way to set something like this up? Preferably that doesn't require the component to render twice (I'm using SSR).
In my case, I'm using NextJS and making an API call to fetch data in getInitialProps. Return of getInitialProps puts data on props. Those props are given to App. When user changes state, the App needs data from state now, not props
foo
with value123
. After which, user action change the value offoo
via redux. Connect maps the store state to props. And state has thefoo
key. – GN.getInitialProps
. Return ofgetInitialProps
puts data on props. Those props are given toApp
. When user changes state, the App needs data from state now, not props. Can you elaborate on how to use thisseparate prop
? – GN.foo
as a prop on the component, let connect pass that prop. If you need a different value there initially, then set the store up with that initial value – John Ruddell