我在做出反应时遇到麻烦

时间:2019-03-20 21:19:39

标签: reactjs

这是我在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。我想要那东西。请帮助我

1 个答案:

答案 0 :(得分:0)

connectmapStateToProps作为第一个参数。如果您不需要这个,只需

export default connect(null, a)(Counter)

Counter需要获取a中定义的mapDispatchToProps并将其用作onClick事件处理程序:

const Counter = ({a}) => {

        return (
                <div>
                        <button onClick={a} ></button>
                </div>
        );
}