typescript - React Router 的参数不起作用

标签 typescript redux react-router

我有一个待办事项应用程序,我正在尝试选择一个项目并将其发送到要编辑的页面。我看过这篇文章Getting query parameters from react-router hash fragment并注意到他们正在使用 this.props.location.query

我想要完成的是获取查询参数并将其显示在输入框中进行编辑。

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;

我假设我的界面不包含位置,因此 props 不知道位置。

以下是我的路线的定义方式:

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>
)

在我的商店被调度后,我正在调用 browserHistory.push('/edit/'+task.index) ,它将我发送到正确的 View ,但我无法访问查询参数。我的其余代码可以在 https://github.com/crh225/HotReactAsp/tree/master/src/HotReactAsp/content 找到谢谢!

最佳答案

您只需打印 this.props 即可查看 props 的呈现方式,并找到提取所需数据的正确方法。

我不知道您使用的是哪个版本的react-router,但据我所知,获取查询参数的正确方法是:

let { id } = this.props.params;

还有这个

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

将会

index: this.props.params.id ? this.props.params.id : 0,

关于typescript - React Router 的参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38666055/

相关文章:

javascript - 我的调用 async/await 在我的操作中返回一个 Promise {<pending>}

javascript - React Stepzilla 中的 React 路由器

javascript - 在 React 中导入文件的不同方式

angular - 后台图片不显示

typescript - 如何以编程方式更改 VSCode 的语言模式?

arrays - 如何使用 Angular 获取所有扑克牌图像并将其添加到数组中?

Angular 2 : Component is not part of any module

javascript - react -Redux; .map 函数有时会重复数组

javascript - 如何在 Redux 中使用类模型(使用 Mobx 选项)

javascript - React Router V4 使用 Redux-persist 和 React-snapshot 保护私有(private)路由