javascript – 在React / Redux中调度操作的问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在React / Redux中调度操作的问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3033字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 在React / Redux中调度操作的问题](/upload/InfoBanner/zyjiaocheng/805/9438d24c5fb643ca8c7f5e97cfed307a.jpg)
我是React和Redux的新手,在我第一次使用它时遇到了一些问题.我尝试按照Redux Doc中的示例进行操作,但我很难理解所有内容,因为每个示例都在ES5 – 6甚至7语法之间跳转.
但是,当我尝试调度操作时,我收到以下错误
Uncaught TypeError: (0 , _index2.default) is not a function
我知道SO社区在一个问题中不喜欢这么多代码,但我不知道问题来自哪里.对不起!
这些是我的代码:
Index.js
import 'babel-polyfill'
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import App from './containers/App'
import configureStore from './store/configureStore'
const store = configureStore()
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
我的商店
import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import index from '../reducers'
export default function configureStore(preloadedState) {
const store = createStore(
index,
preloadedState,
applyMiddleware(thunkMiddleware, createLogger())
)
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers').default
store.replaceReducer(nextRootReducer)
})
}
return store
}
我的容器组件
import React, { Component, PropTypes } from 'react'
import AddTodo from '../components/AddTodo'
import { connect } from 'react-redux'
import addItem from '../actions/index'
class App extends Component {
constructor(props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
handleClick(e){
console.log("click")
console.log(e);
const {dispatch} = this.props
dispatch(addItem(e));
}
render() {
return (
<div>
< h1 > Hallo </h1>
<AddTodo handleAddItem={this.handleClick}/>
</div>
)
}
}
App.propTypes = {
dispatch: PropTypes.func.isRequired
}
function mapStateToProps(state){
return {
AddTodo
}
}
export default connect (mapStateToProps)(App)
我的孩子成分:
import React, { Component, PropTypes } from 'react'
import addItem from '../actions/index'
export default class AddTodo extends Component {
constructor(props) {
super(props)
this.handleClick = this.handleClick.bind(this)
this.state = {newItem: ''}
}
onChange(e){
console.log("change")
console.log(e.target.value);
this.setState({newItem: e.target.value})
}
handleClick(e){
this.props.handleAddItem(this.state.newItem)
// const {dispatch} = this.props
// console.log("clickc")
// console.log(this.state.newItem);
// dispatch(addItem(this.state.newItem))
}
render() {
return (
<div>
<h3>Add Item </h3>
<input
type="text"
value={this.state.newItem}
onChange={this.onChange.bind(this)}
/>
<button onClick={this.handleClick}>Hallo</button>
</div>
)
}
}
减速机
export default (state = [], action) => {
switch (action.type){
case 'ADD_ITEM':
return action.item
}
}
最后是行动
export function addItem(item){
console.log("addTOdo")
return {
type: 'ADD_ITEM',
item
}
}
我希望有人可以在这里帮助我,坐几个小时,不知道发生了什么.
解决方法:
您没有将动作创建者导出为默认值.你需要
export default function addItem(item){
console.log("addTOdo")
return {
type: 'ADD_ITEM',
item
}
}
要么
import {addItem} from '../actions/index'
内容总结
以上是互联网集市为您收集整理的javascript – 在React / Redux中调度操作的问题全部内容,希望文章能够帮你解决javascript – 在React / Redux中调度操作的问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。