私はReduxを学ぼうとしています。私はオンラインのチュートリアルに従い、その例を使用してうまくいきました。それから私は別の小さなテストアプリを作って、それは正常に働いた。今私は別の小さなテストアプリを試していると私はこのエラーに立ち往生し、理由が分からない。Redux - レデューサーが呼び出されない
アプリケーションは単なる入力ボックスとボタンです。ボタンをクリックすると、ボックス内の内容が下に表示されるリストに追加されます。
ただし、レデューサーは状態を更新していません。私はこれを引き起こす一般的な問題を見てきましたが、私の減速機は状態を変えず、私はディスパッチなどに拘束しました。たぶん私はどこか(またはそのような小さなと愚かな)どこかで誤植をしただけで、私はそれを働かせることはできません。
私はそれを変更しようとしました。下のボックスに入力した文字列が表示され、それでも動作しません。誰でもなぜ減速機が作動していないのか知っていますか?
index.js(メイン)
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import promise from 'redux-promise';
import createLogger from 'redux-logger';
import allReducers from './reducers';
import App from './components/App';
const logger = createLogger();
const store = createStore(
allReducers,
applyMiddleware(thunk, promise, logger)
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
App.js
import React from 'react';
import NameList from '../containers/name-list.js'
import Input from '../containers/input.js'
const App =() => (
<div>
<Input />
<hr />
<NameList />
</div>
);
export default App;
index.js(アクション)
export const addName = (name) => {
return {
type: 'NAME_ADDED',
payload: name
}
};
減速・アドインname.js
export default function (state = null, action) {
switch (action.type) {
case 'NAME_ADDED':
return action.payload;
break;
}
return state;
}
index.js(減速合成)
import {combineReducers} from 'redux';
import AddNameReducer from './reducer-add-name';
const allReducers = combineReducers({
nameList: AddNameReducer
});
export default allReducers
input.js
import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {addName} from '../actions/index'
class Input extends Component {
render() {
return(
<div>
<input id='name-input' />
<button id='add-name-button' onClick={() => addName(document.getElementById('name-input').value)}>Add name</button>
</div>
);
}
}
function mapStateToProps(state) {
return {
nameList: state.nameList
};
}
function matchDispatchToProps(dispatch) {
return bindActionCreators({addName: addName}, dispatch);
}
export default connect(mapStateToProps, matchDispatchToProps)(Input);
名list.js
import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
class NameList extends Component {
getNameList() {
//This is where I'll loop through this.props.nameList and build the list of elements
return this.props.nameList;
}
render() {
return(
<div>Current list : {this.getNameList()}</div>
);
}
}
function mapStateToProps(state) {
return {
nameList: state.nameList
};
}
export default connect(mapStateToProps)(NameList);
ありがとうございました!
コンソールのエラーを確認してください。また、あなたの減速機が(index.jsでインポートされた)AddNameReducerをエクスポートするのを見ることができませんが、あなたは実際のコードではなく、これを逃したかもしれません。 –
コンソールエラーはありません。 AddNameReducerは、エクスポートされた単一の関数であるreducer-add-name.jsファイルです。 – Jayce444
input.jsのconnect関数は、dispatchメソッドをInputコンポーネントのpropsにマッピングします。だから、Inputコンポーネントのrender関数がaddNameを呼び出すと、これを小道具として参照することになるでしょうか? (だからthis.props.addNameを試してください) –