2

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!

Community
  • 1
  • 1
crh225
  • 821
  • 18
  • 34

2 Answers2

2

You could just print your this.props to look how the props are presented and find a correct way to extract needed data.

I don't know which version of react-router you're using but as I know the correct way to get query params is:

let { id } = this.props.params;

and this

index: this.props.task ? this.props.task.index : 0,

will be

index: this.props.params.id ? this.props.params.id : 0,
shadeglare
  • 7,006
  • 7
  • 47
  • 59
1

I'm sure you've solved this by now but you need to extend your interface with routerProps. You'll need @types:react-router-dom

Import { withRouter, Switch, Route, NavLink, RouteProps } from 'react-router-dom';

interface TaskEditProp extends RouteProps {
    task: ITask
}
JamesTBennett
  • 2,091
  • 6
  • 19
  • 22