私はReact、Redux、およびMongoDBを使用してほとんどアプリケーションを作成していません。 残念ながら私は腋窩とreduxを使用することに問題があります。私はそれをこのように減らしてみました:reduxでAxiosを使用する適切な方法
export function users(state = initialState, action) {
switch (action.type) {
case 'USER_ADD':
{
axios.post('http://localhost:4200/users/add/post', {user:
action.user}).then(() => {
return {
...state,
items: [
...state.users,
action.user
]
}
}).catch(err => console.log(err));
break;
}
.........
しかし、それは動作しません。それから私は私の行動のクリエイターにaxiosを移動し、それは次のようになります。それは、モンゴデータベースに新しい文書をポストが、それはまた、私にエラーを与える
export function addUser(user) {
return function (dispatch) {
axios.post('http://localhost:4200/users/add/user', {user:user})
.then(() => dispatch({
type: USER_ADD,
user: user
})).catch(err => console.log(err));
}
}
:アクションは、プレーンなオブジェクトでなければなりません。非同期アクションにはカスタムミドルウェアを使用します。はい、私はreduxサンクを使用しています;)
誰が問題をどこに教えていただけますか?他のコードが役に立つかどうかは分かりませんが、もっとコードを求めてください。
EDIT:
私はそのようにReduxの-サンクを使用しています:
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)
(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>,
document.getElementById('root')
);
redux-thunkをインポートしましたか? – WitVault
はい、どのように私が行ったかを示すために私の投稿を編集しています – Kreha
サイドノート:リダクターは副作用のない純粋な機能でなければなりません。 –