简体   繁体   English

使用redux-saga重定向React路由器

[英]Redirect react router with redux-saga

I have redux-saga which should redirect user to main page after successfully logging. 我有redux-saga,应该在成功登录后将用户重定向到主页。 I do not use react-router-redux or smth like that so my router is not connected to state. 我不使用类似的react-router-redux或smth,因此我的路由器未连接到状态。

Here is my Login component 这是我的登录组件

const AdminLogin: React.FC<RouteComponentProps> = ({history}) => {
    const [form, setForm] = useState({username: '', password: ''});
    const user = useSelector(getAdminUser);
    const dispatch = useDispatch();
    const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();
        dispatch(makeLoginAsync(form));
    };

    useEffect(() => {
        if (user && user.token) {
            history.push('/admin/main'); // here it is
        }
    }, [user, history]);


    return (
        <form onSubmit={handleSubmit} className="admin-login">
            // FORM HTML CODE
        </form>
    );
};

export default withRouter(AdminLogin);

Notice I'm dispatching form to Action, then Saga is listening my Action and does all logic and effects. 注意,我正在将表单分派给Action,然后Saga监听我的Action并执行所有逻辑和效果。 I thought it would be much better to do redirection in Saga code. 我认为用Saga代码进行重定向会更好。 But I couldn't? 但是我不能吗

After successful login, Reducer changes the state sets user auth token. 成功登录后, Reducer会更改状态集用户身份验证令牌。 If I have token, I take it as user is logged in and redirect him to another page. 如果我有令牌,则在用户登录时将其视为令牌,并将其重定向到另一个页面。

As you see, I implement this logic directly in the component (using Selector). 如您所见,我直接在组件中(使用Selector)实现了此逻辑。 But I find it very ugly. 但是我觉得这很丑。

Actually, I can access history object only in component (if component is wrapped with withRouter ). 实际上,我只能在component中访问history对象(如果component用withRouter包装)。

If I used react-router-redux , I could do something like 如果我使用react-router-redux ,我可以做类似的事情

import { push } from 'react-router-redux'

and use push function to redirect from Saga. 并使用push功能从Saga重定向。 I'm new to React and I heard that it's not obligatory to connect router to redux. 我是React的新手,听说没有必要将路由器连接到Redux。 Also I don't want to make my application more complex and have another dependency to implement such basic feature as redirection. 另外,我也不想使我的应用程序更加复杂,而又不想再依赖于实现诸如重定向之类的基本功能。

So now I have to make it even more complex and implicitly. 所以现在我必须使它变得更加复杂和隐式。 Maybe I have missed something? 也许我错过了什么?

Any help is welcome. 欢迎任何帮助。

pass history with dispatch event and then use it to push route inside redux 通过调度事件传递历史记录,然后使用它在redux中推送路由

const AdminLogin: React.FC<RouteComponentProps> = ({history}) => {
    const [form, setForm] = useState({username: '', password: ''});
    const user = useSelector(getAdminUser);
    const dispatch = useDispatch();
    const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();
        dispatch(makeLoginAsync({form,history}));
    };

    useEffect(() => {
        if (user && user.token) {
            history.push('/admin/main'); // here it is
        }
    }, [user, history]);


    return (
        <form onSubmit={handleSubmit} className="admin-login">
            // FORM HTML CODE
        </form>
    );
};

export default withRouter(AdminLogin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM