gpt4 book ai didi

javascript - React Hooks 与 React Router v4 - 我如何重定向到另一条路线?

转载 作者:数据小太阳 更新时间:2023-10-29 05:17:16 26 4
gpt4 key购买 nike

我有一个简单的 React Hooks 应用程序 - Todos 列表 - 带有 React Router v4

在 Todo 列表中,当点击 Todo 时我需要:

  1. 在上下文中调度当前的待办事项
  2. 重定向到另一条路线(从/todos 到/todos/:id)

在之前基于 React 类的实现中,我可以使用 this.context.history.push 重定向到另一个路由。

我将如何结合使用 React Hooks 和 React Router v4 来处理这个问题(在下面的代码中,请参阅我在函数 editRow() 中的评论)?

代码如下:

=====index.js=====

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter} from "react-router-dom"

import App from './App';

ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>, document.getElementById('root'));

=====main.js=====

import React from 'react'
import { Switch, Route } from 'react-router-dom'
import TodosList from './todoslist'
import TodosEdit from './todosedit'

const Main = () => (
<main>
<Switch>
<Route exact path="/todos" component={TodosList}/>
<Route exact path="/todos/:id" component={TodosEdit} />
</Switch>
</main>
)

export default Main

=====app.js=====

import React, {useContext, useReducer} from 'react';
import Main from './main'
import TodosContext from './context'
import todosReducer from './reducer'

const App = () => {
const initialState = useContext(TodosContext);
const [state, dispatch] = useReducer(todosReducer, initialState);
return (
<div>
<TodosContext.Provider value={{state, dispatch}}>
<Main/>
</TodosContext.Provider>
</div>
)
}
export default App;

=====TodosContext.js=====

import React from 'react'

const TodosContext = React.createContext({
todos: [
{id:1, text:'Get Grocery', complete:false},
{id:2, text:'Excercise', complete:false},
{id:3, text:'Drink Water', complete:true},
],
currentTodo: {}
})

export default TodosContext

=====reducer.js=====

import React from 'react'

export default function reducer(state, action){
switch(action.type){
case "GET_TODOS":
return {
...state,
todos: action.payload
}
case "SET_CURRENT_TODO":
return {
...state,
currentTodo: action.payload
}
default:
return state
}
}

=====Todos.js=====

import React, {useState, useContext, useEffect} from 'react';
import TodosContext from './context'

function Todos(){
const [todo, setTodo] = useState("")
const {state, dispatch} = useContext(TodosContext)
useEffect(()=>{
if(state.currentTodo.text){
setTodo(state.currentTodo.text)
} else {
setTodo("")
}
dispatch({
type: "GET_TODOS",
payload: state.todos
})
}, [state.currentTodo.id])

const editRow = event =>{
let destUrlEdit = `/todos/${event.id}`

let obj = {}
obj.id = event.id
obj.text = event.text

dispatch({type:"SET_CURRENT_TODO", payload: obj})

//after dispatch I would like to redirect to another route to do the actual edit
//destUrlEdit
}
return(
<div>
<h1>List of ToDos</h1>
<h4>{title}</h4>
<ul>
{state.todos.map(todo => (
<li key={todo.id}>{todo.text} &nbsp;
<button onClick={()=>{
editRow(todo)}}>
</button>
</li>
))}
</ul>
</div>
)
}

export default Todos;

最佳答案

它实际上比其他答案简单很多,React Router v5.1 提供了一个 useHistory钩子(Hook)。

import React from 'react'
import { useHistory } from 'react-router-dom'

const MyComponent = () => {
const history = useHistory()
const handleButtonClick = (event) => {
history.push(event.target.value)
}
return (
<button
type="button"
value="/my/path"
onClick={handleButtonClick}
>
Navigate Me!
</button>
)
}

关于javascript - React Hooks 与 React Router v4 - 我如何重定向到另一条路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54579730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com