这是我在github上的第一篇文章。我只会说一点英语,所以我很抱歉。运行ALERT。我想要那东西。请帮助我
这是我的应用页面
import React from 'react';
import {Provider} from 'react-redux';
import Counter from './Counter';
import {createStore} from 'redux';
const initialState = {
count : 0
};
function reducer( state = initialState, action){
if(action.type === "ALERT" ){
alert("ahihi");
}
return state
}
const store = createStore(reducer);
const App = () => (
<Provider store ={store}>
<Counter />
</Provider>
);
export default App;
这是我的“计数器”页面
import React from 'react';
import {connect} from "react-redux"
import {bindActionCreators} from 'redux';
export const ahihi = () => ({
type: "ALERT"
});
const Counter = ({ab}) => {
return (
<div>
<button onClick={ALERT} ></button>
</div>
);
}
const a = (dispatch) =>({
a : bindActionCreators(ahihi, dispatch)
}
export default connect(a)(Counter)
运行ALERT。我想要那东西。请帮助我
答案 0 :(得分:0)
connect
以mapStateToProps
作为第一个参数。如果您不需要这个,只需
export default connect(null, a)(Counter)
Counter
需要获取a
中定义的mapDispatchToProps
并将其用作onClick
事件处理程序:
const Counter = ({a}) => {
return (
<div>
<button onClick={a} ></button>
</div>
);
}