Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

javascript - How to solve "Uncaught TypeError: Cannot read property 'params' of undefined" reactjs + django

i'm practicing reactjs watching this video https://www.youtube.com/watch?v=5rh853GTgKo&list=PLJRGQoqpRwdfoa9591BcUS6NmMpZcvFsM&index=9

I want to verify my information using uid and token, but I don't know how to deliver it.

In this code: Activate.js in container

import React, { useState } from 'react';
import { Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { verify } from '../actions/auth';

const Activate = ({ verify, match }) => {
const [verified, setVerified] = useState(false);

const verify_account = e => {
  const uid = match.params.uid; // I Think This part is Problem
  const token = match.params.token;

  verify(uid, token);
  setVerified(true);
};


if (verified) {
   return <Redirect to='/' />
}

and this code : auth.js in actions

export const verify = (uid, token) => async dispatch => {
  const config = {
    headers: {
      'Content-Type': 'application/json'
    }
  };

  const body = JSON.stringify({ uid, token });

  try {
    await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/activation/`, body, config);

    dispatch ({
      type: ACTIVATION_SUCCESS,
    });
  } catch (err) {
    dispatch ({
      type: ACTIVATION_FAIL
    });
  }
}

i think i didn't render uid, token but i confused how to do that

App.js code:

<Router>
  <Layout>
    <Switch>
      <Route exact path ='/activate/:uid/:token'>
        <Activate />
      </Route>
    </Switch>
  </Layout>
</Router>

I'd appreciate any help. :)

question from:https://stackoverflow.com/questions/66068424/how-to-solve-uncaught-typeerror-cannot-read-property-params-of-undefined-re

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use the useParams hook to extract uid and token params:

import React, { useState } from 'react';
import { Redirect, useParams } from 'react-router-dom';
import { connect } from 'react-redux';
import { verify } from '../actions/auth';

const Activate = ({ verify }) => {
const [verified, setVerified] = useState(false);
const { uid, token } = useParams();

const verify_account = e => {
  verify(uid, token);
  setVerified(true);
};


if (verified) {
   return <Redirect to='/' />
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...