使用ARc / React / Redux

时间:2017-04-19 18:05:04

标签: javascript reactjs redux redux-saga

我使用https://arc.js.org/设置新项目,并对如何传递数据感到困惑。我只是使用表单发布一些登录数据,并且似乎无法将数据发送到帖子本身的行动/传真(意思是,我的登录表单组件有数据并尝试发送它,但在调度操作后,值未定义)。

打算将表单值放到redux store中(更新onChange可以在onSubmit上访问,而不是像我现在那样放弃),但是想让这个版本先工作然后再继续下去,这样我就知道了什么'实际上正在发生。

如果在这里缺少必要的信息,请告诉我。

LoginFormContainer:

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { userLoginRequest } from 'store/actions'
import { fromUser } from 'store/selectors'
import { LoginForm } from 'components'

class LoginFormContainer extends Component {
  static propTypes = {
    login: PropTypes.func.isRequired,
  }

  onSubmit = (event) => {
    event.preventDefault()
    const serialize = new FormData(event.target)
    const loginData = {
      email: serialize.get('email'),
      password: serialize.get('password'),
    }
    this.props.login(loginData)
  }

  render() {
    return <LoginForm handleSubmit={this.onSubmit} />
  }
}

const mapStateToProps = (state) => ({
  user: fromUser.getUser(state),
})

const mapDispatchToProps = (dispatch, { loginData }) => ({
  login: () => dispatch(userLoginRequest(loginData)),
})

export default connect(mapStateToProps, mapDispatchToProps)(LoginFormContainer)

LoginFormComponent:

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'

import { Field } from 'components'

const Form = styled.form`
  width: 100%;
`

const LoginForm = ({ handleSubmit }) => {
  return (
    <Form onSubmit={handleSubmit}>
      <Field
        label="Email"
        name="email"
        type="text"
      />
      <Field
        label="Password"
        name="password"
        type="text"
      />
      <button type="submit">Login</button>
    </Form>
  )
}

LoginForm.propTypes = {
  handleSubmit: PropTypes.func.isRequired,
  handleChange: PropTypes.func.isRequired,
}

export default LoginForm

操作:

export const USER_LOGIN_REQUEST = 'USER_LOGIN_REQUEST'
export const USER_LOGIN_SUCCESS = 'USER_LOGIN_SUCCESS'
export const USER_LOGIN_FAILURE = 'USER_LOGIN_FAILURE'

// This doesn't know what data is (undefined)
export const userLoginRequest = (data, resolve, reject) => ({
  type: USER_LOGIN_REQUEST,
  data,
  resolve,
  reject,
})

export const userLoginSuccess = detail => ({
  type: USER_LOGIN_SUCCESS,
  detail,
})

export const userLoginFailure = error => ({
  type: USER_LOGIN_FAILURE,
  error,
})

萨加斯:

import { take, put, call, fork } from 'redux-saga/effects'
import api from 'services/api'
import * as actions from './actions'

// This doesn't know what loginData is (undefined)
export function* login(loginData) {
  try {
    const encoded = window.btoa(`${loginData.email}:${loginData.password}`)
    const data = yield call(api.post, '/login', { Authorization: `Basic ${encoded}` })
    yield put(actions.userLoginSuccess(data))
  } catch (e) {
    yield put(actions.userLoginFailure(e))
  }
}
export function* watchUserLoginRequest() {
  while (true) {
    const { data } = yield take(actions.USER_LOGIN_REQUEST)
    yield call(login, data)
  }
}

export default function* () {
  yield fork(watchUserLoginRequest)
}

1 个答案:

答案 0 :(得分:3)

感谢@dagatsoin帮助引导正确方向!

mapDispatchToProps应为:

const mapDispatchToProps = (dispatch) => ({
  login: (loginData) => dispatch(userLoginRequest(loginData)),
})