【发布时间】:2017-03-25 19:40:49
【问题描述】:
刚开始使用 Redux 编写自定义内容,我想将输入传递给我的操作。
(同样使用本教程:https://www.youtube.com/watch?v=kNkTQtRUH-M)
基本上,我想使用this.props.testUserPhone('+1**********') 将电话号码(在示例中:'+1**********')传递给我的操作,但我收到了this.props.testUserPhone is not a function 错误.
我在这里做错了什么?我觉得有一种特定的方法可以使用我丢失的参数执行函数,或者我的绑定错误或其他什么。
phonepage.js
@connect(
state => ({
testUserPhone: state.phonepage.testUserPhone
}),
{ testUserPhone }
)
class PhonePage extends Component {
componentDidMount() {
this.props.testUserPhone('+1**********')
}
render() {
console.log(this.props.testUserPhone('+1**********'))
return(
// Render stuff
)
}
}
actions.js
import { UserAPI } from '../../constants/api'
const userAPI = new UserAPI()
export const TEST_USER_PHONE = 'TEST_USER_PHONE'
export const testUserPhone = (user) => ({
type: TEST_USER_PHONE,
payload: userAPI.testPhone(user)
})
reducer.js
import {
TEST_USER_PHONE
} from './actions'
const INITIAL_STATE = {
testedByPhone: {
data: [],
isFetched: false,
error: {
on: false,
message: null
}
}
}
export default (state = INITIAL_STATE, action) => {
switch(action.type) {
case '${TEST_USER_PHONE}_PENDING':
return INITIAL_STATE
case '${TEST_USER_PHONE}_FULFILLED':
return {
testedByPhone: {
data: action.payload,
isFetched: true,
error: {
on: false,
message: null
}
}
}
case '${TEST_USER_PHONE}_REJECTED':
return {
testedByPhone: {
data: [],
isFetched: true,
error: {
on: true,
message: 'Error when fetching testedByPhone'
}
}
}
default:
return state
}
}
reducers.js
import { combineReducers } from 'redux'
import {
phonereducer
} from '../scenes'
export default combineReducers({
phonepage: phonereducer
})
【问题讨论】:
标签: reactjs react-native redux react-redux