动作必须是普通对象。即使安装了thunk,也可以使用自定义中间件进行异步操作

时间:2018-01-20 07:05:22

标签: reactjs react-redux react-redux-form

我们有3个减速器,其中1个是过滤减速器,其余2个用于api调用目的。我想知道的事情:我们可以在一个应用程序中使用多个异步缩减器。这是我的代码,我得到运行时错误

商品



import {createStore, combineReducers, applyMiddleware} from 'redux';
import thunk from 'redux-thunk'
import {composeWithDevTools} from 'redux-devtools-extension';
import reportsReducer from '../reducers/reportreducer'
import filterReducer from '../reducers/filterreducer'
import testReducer from '../reducers/testreducer'


export default () => {
    const store = createStore(
        combineReducers({
            reports: reportsReducer,
            tests:testReducer,
            filters: filterReducer,
        }),
        composeWithDevTools(
            applyMiddleware(thunk)
        )
        
    )
    return store;
}



 的操作



import thunk from 'redux-thunk'

export const addtest=(tests)=>{
type: 'ADD_TEST_UNIT',
tests
}



export const saveTests = (testData = {}) => {
    return dispatch =>{
        const {
            t_id=0,
            test_name= '',
            print_name= '',
            department= '',
            test_individual= 0,
            std_rate= '',
            prt_tst_np= 0,
            discon_prcls=0,
            unit= '',
            res_lst= 0,
            num_rng= 0,
            wrd_tempt= 0,
            tst_mthd= '',
            interpret= '',
            for_bsd_tst= 0,
        } = testData
        const tests = {t_id, test_name, print_name, department, test_individual, std_rate, prt_tst_np, discon_prcls, unit, res_lst, num_rng, wrd_tempt, tst_mthd, interpret, for_bsd_tst}
    
        return fetch('http://localhost/aruna/addtestunits.php',{
            method:'POST',
            body: JSON.stringify(testData)
        }).then(testData => dispatch(addtest(testData.tests))
    )}
}




减速



const testsDefaultstate = []

export default (state=testsDefaultstate, action ) => {
    switch (action.type){
        case 'ADD_TEST_UNIT':
            return [
                ...state,
                action.testunit
            ]
        
        default:
            return state
    }
}




FORM



import React from 'react';
import {connect} from 'react-redux';
import {saveTests} from '../../actions/testaction';
class TestUnitForm extends React.Component {
    state={
        // t_id=null,
        test_name: '',
        print_name: '',
        department: '',
        test_individual: 0,
        std_rate: '',
        prt_tst_np: 0,
        discon_prclst: 0,
        unit: '',
        res_lst: 0,
        num_rng: 0,
        wrd_tempt: 0,
        tst_mthd: '',
        interpret: '',
        for_bsd_tst: 0,
        loading: 0,
        done: false
    }
   handlerChange=(e) => {
    this.setState({[e.target.name]: e.target.value})
 }
 onSubmit=(e)=>{
    e.preventDefault();
    const {test_name,print_name, department, test_individual,std_rate, prt_tst_np, discon_prclst, unit,res_lst, num_rng, wrd_tempt, tst_mthd,interpret, for_bsd_tst}=this.state;
    //  this.setState({loading:true});
     this.props.saveTests({test_name,print_name, department, test_individual,std_rate, prt_tst_np, discon_prclst, unit,res_lst, num_rng, wrd_tempt, tst_mthd,interpret, for_bsd_tst}).then(() => {this.setState({done:true})})
     
 }
    render() {
        const form =(
            <form onSubmit={this.onSubmit}>
                <input type="text" value={this.state.test_name} name="test_name" placeholder="Test Name" onChange={this.handlerChange}/>
                <input type="text" value={this.state.print_name} name="print_name" placeholder="Print Name" onChange={this.handlerChange}/>
                <input type="text" value={this.state.department} name="department" placeholder="Department" onChange={this.handlerChange} />
                <label for="test_individual">Test Can Be Requested Individually </label> 
                <input type="checkbox" value={this.state.test_individual} name="test_individual" onChange={this.handlerChange} />
                
                <input type="text" value={this.state.std_rate} name="std_rate" placeholder="Standard Rate" onChange={this.handlerChange}/>
                <label for="prt_tst_np"> Print Test on New Page </label>
                <input type="checkbox" value={this.state.prt_tst_np} name="prt_tst_np" onChange={this.handlerChange} />
                <label for="discon_prclst"> Discontinue from Price List</label>
                <input type="checkbox" value={this.state.discon_prclst} name="discon_prclst" onChange={this.handlerChange} />
                <input type="text" value={this.state.unit} name="unit" placeholder="Unit" onChange={this.handlerChange} />
                <label for="res_lst"> Result List </label>
                <input type="checkbox" value={this.state.res_lst} name="res_lst" onChange={this.handlerChange}/>
                <label for="num_rng"> Numeric Range</label>
                <input type="checkbox" value={this.state.num_rng} name="num_rng" onChange={this.handlerChange} />
                <label for="wrd_tempt">Work Template</label>
                <input type="checkbox" value={this.state.wrd_tempt} name="wrd_tempt" onChange={this.handlerChange}/>
                <input type="text" value={this.state.tst_mthd} name="tst_mthd" placeholder="Test Method" onChange={this.handlerChange} />
                <input type="text" value={this.state.interpret} name="interpret" placeholder="Interpretation" onChange={this.handlerChange} />
                <label for="for_bsd_tst"> Formula Based Test </label>
                <input type="checkbox" value={this.state.for_bsd_tst} name="for_bsd_tst" onChange={this.handlerChange} />
                <input type="submit" value="Save Report" />
            </form>
        )
        return (
            <div>
                {form}
            </div>
        )
    }
}
export default connect(null,{saveTests})(TestUnitForm);
&#13;
&#13;
&#13;

如何删除此错误。在一个减速器中,它正在工作,而在另一个减速器中,它显示出这个错误

1 个答案:

答案 0 :(得分:1)

在你的行动中,你没有返回任何东西:

    def SearchRecords(self):
    search = raw_input("Insert value to be searched in the DB:")
    db = MySQLdb.connect("localhost", "root", "studio", "PManDB")
    cursor = db.cursor()
    sql = """SELECT * FROM accounts WHERE website LIKE %s"""
    data = ('%' + search + '%',)
    cursor.execute(sql, data)
    result = cursor.fetchall()
    print result

这只是一个函数的主体,你需要将它包装成括号:

export const addtest = (tests) => {
  type: 'ADD_TEST_UNIT',
  tests
}

你的问题应该修复;)

如果不清楚我可以告诉我,我可以进一步解释。