I have a todo app that I am trying to select an item and send it over to a page to be edited. I have taken a look at this post Getting query parameters from react-router hash fragment and noticed they are using this.props.location.query
What I am trying to accomplish is to get the query param and display it in the input box for editing.
import * as React from 'react'
import { Router, Route, IndexRoute, hashHistory, browserHistory, Link } from 'react-router'
import {ITask, IStoreAction, ActionType} from '../model/TasksModel'
import store from '../store/Store'
interface TaskEditProp {
task: ITask
}
class TaskEdit extends React.Component<TaskEditProp, {}>{
constructor() {
super();
this.state = store.getState()
store.subscribe(() => {
this.setState(store.getState())
})
//todo: console.log(this.props.location);
}
onSave = (e) => {
e.preventDefault();
var refs: any = this.refs;
var task: ITask = {
index: this.props.task ? this.props.task.index : 0,
text: refs.text.value,
done: false
}
var storeAction: IStoreAction = {
type: ActionType.EDIT_TASK,
task
}
store.dispatch(storeAction)
browserHistory.push('/')
}
onCancel = (e) => {
e.preventDefault();
browserHistory.push('/')
}
render() {
const { props: { children } } = this;
return (
<div className="">
<h3>Edit Task</h3>
<form onSubmit={this.onSave}>
<fieldset className="form-group">
<label for="task">Task</label>
<input type="text" ref="text" className="form-control" >{this.props.location}</input>
</fieldset>
<div className="btn-group">
<button className="btn btn-primary" >Save</button>
<button className="btn btn-warning" onClick={this.onCancel} >Cancel</button>
</div>
</form>
</div>
)
}
}
export default TaskEdit;
I am assuming my interface does not contain location so props does not know about location.
Here are how my routes are defined:
var Routes = (
<Router history={browserHistory}>
<Route path="/" component={Tasks}>
<IndexRoute component={TaskList} />
<Route path="/add" component={TaskAdd} />
<Route path="/edit/:id" component={TaskEdit} />
</Route>
<Route path="/about" component={About}/>
</Router>
)
Afer my store has been dispatched, I am calling browserHistory.push('/edit/'+task.index)
which send me to the correct view, but I cannot access the query params. The rest of my code can be found at https://github.com/crh225/HotReactAsp/tree/master/src/HotReactAsp/content Thanks!