javascript - React/Redux 无法读取未定义的属性 'setCurrent'

标签 javascript reactjs redux react-redux

所以我正在为我的项目准备 redux 并陷入困境。 所有事情看起来都是正确的,因为 redux devtools 显示了状态。

当我尝试通过 this.props.function_name 调用组件中的函数时出现问题

我在 .then() 中进行了该调用,因为我在 axios 返回 token 时调用它,我知道该范围在 then 中发生变化,但我在 then 中使用箭头函数,所以问题似乎不存在.

还尝试从另一个函数调用 this.props.setcurrent ,但是得到了 _this2 未定义 我的代码:

Signup.js

import {setCurrent} from '../actions/authActions'

class SignUp extends Component {
  constructor(props) {
    super(props);
  }
  responseGoogle(response) {
    console.log('google', response);
    const access_token = response.Zi.access_token;
    axios
      .post('http://localhost:3001/users/oauth/google', {
        access_token
      })
      .then((response) =>{
        console.log('google', response);
        const decoded = jwt_decode(response.data.token);
        this.props.setCurrent(decoded);
        console.log(decoded);
      })
      .catch(function (error) {
        console.log(error);
      });
  }
  render() {
    return (
          <GoogleLogin
            clientId="890644813294-bvuq6cf7lsilohneqvov28oi60sfdmig.apps.googleusercontent.com"
            buttonText="Login"
            onSuccess={this.responseGoogle}
            onFailure={this.responseGoogle}
          />
       )
  }
}

export default connect(null, { setCurrent })(SignUp);

authActions.js

import { TEST_DISPATCH } from './types';

// Testing
export const setCurrent = (userToken) => {
  return {
    type: TEST_DISPATCH,
    payload: userToken
  };
};

authReducer.js

import { TEST_DISPATCH } from '../actions/types';
const initialState = {
  isAuthenticated: false,
  user: {}
}
export default function(state = initialState, action) {
  switch(action.type) {
    case TEST_DISPATCH:
      return {
        ...state,
        user: action.payload
      };
    default:
      return state;
  }
}

Auth/index.js

import { combineReducers } from 'redux';
import authReducer from './authReducer';

export default combineReducers({
  auth: authReducer
});

store.js

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState = {};

const middleware = [thunk];

const store = createStore(
  rootReducer,
  initialState,
  compose(
    applyMiddleware(...middleware),
   // window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  )
);

export default store;

App.js

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import store from '../store';
import LogIn from './LogIn';
import SignUp from './SignUp';


class App extends Component {
  render() {
    return(
      <Provider store={ store }>
      <div>
      <h1>SignUp!</h1>
      <SignUp />
    </div>
    </Provider>
    )
  }
}
export default App;

完整代码:https://github.com/ExadelPraktika/Front-exabook/tree/auth 我还在 webpack 中使用 babel

最佳答案

i know that scope of this changes in then, but I'm using arrow function with then so the problem seems not to be there.

这仍然是一个范围问题。方法responseGoogle 是有作用域的。所以你需要像这样“自动绑定(bind)”它:

responseGoogle = (response) => {/* 您的代码 */}

在 SignUp 组件的渲染方法中:

<GoogleLogin
    onSuccess={response => this.responseGoogle(response)}
    onFailure={response => this.responseGoogle(response)}
/>

关于javascript - React/Redux 无法读取未定义的属性 'setCurrent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51253413/

相关文章:

javascript - 如何向 JavaScript 变量添加值?

javascript - Keycloak Script base authenticator 失败时出现相同的错误消息

javascript - 如何从 Next.js 中的 Formik 提交返回 API(获取)数据

javascript - 将键值对添加到 redux store

javascript - 从javascript获取python函数的值

javascript - 文本省略号以外的其他选项

javascript - 将变量从函数传递到 React return() 函数

reactjs - React 路由器在 firebase 上托管时不会路由流量

javascript - featureModule 中的 Ngrx meta-reducer 处理所有 featureModule 的操作

javascript - 为 React Component Props 解构 ImmutableJS Map