So you want to keep your image infos between view. By "view", do you mean a different HTML page? I guess a more standard way of doing things would be to:
- store the file content/path in a state (e.g. redux state)
- use client-side router (e.g. react-router) to change the view while keeping the state
If you have never used client-side routing, it looks like that (with react-router):
import { Router, Route, browserHistory } from 'react-router'
// Iy you use redux to handle the state
import { createStore } from 'redux'
import myReducer from 'my-reducer'
const store = createStore(myReducer)
const history = syncHistoryWithStore(browserHistory, store)
// Your view components
function Top(props) { ... }
function UploadImage(props) { ... }
function EditImage(props) { ... }
// The Router itself
ReactDOM.render(
<Router history={history}>
<Route path="/" component={Top} >
<Route path="upload-image" component={UploadImage} />
<Route path="edit-image" component={EditImage} />
</Route>
</Router>)
If you have never used redux before, you can use it this way:
First, create the reducer
import { combineReducers } from 'redux'
const myReducer = combineReducers({ imagePath })
// This is called a reducer function
function imagePath(oldState = "", action) {
switch(action.type) {
case 'SET_PATH':
return action.payload
}
}
Next, connect your component to get the state value (e.g. UploadImage)
const ReduxUploadImage = connect(function(state) {
return {
imagePath: state.imagePath
}
})(UploadImage)
Now if you use ReduxUploadImage
instead of UploadImage
, you can access imagePath
through props.imagePath
. The connect
function also add a dispatch
function to your props.
Finally, you can set the path by calling within your component (but not the render function itself: this would be an anti-pattern)
props.dispatch( { type: 'SET_PATH', payload: "the_path" } )
Finally, it is easy to persist the redux state between pages or refresh using dedicated middleware.