Reactjs 未调用Action Reducer函数

Reactjs 未调用Action Reducer函数,reactjs,redux,react-redux,react-hooks,Reactjs,Redux,React Redux,React Hooks,我正在尝试从django REST api获取,但dispatch action reducer无法工作。我使用的是材质UI、钩子和Redux 动作减速器代码: import axios from 'axios'; import { GET_NOTE } from './types'; // GET NOTE export const getNote = () => (dispatch) => { console.log('Contacting API'); a

我正在尝试从django REST api获取,但dispatch action reducer无法工作。我使用的是材质UI、钩子和Redux

动作减速器代码:

import axios from 'axios';

import { GET_NOTE } from './types';

// GET NOTE
export const getNote = () => (dispatch) =>  {
    console.log('Contacting API');
    axios.get('/api/note/')
        .then(response => {
            console.log('api contact');
            dispatch({
                type: GET_NOTE,
                payload: response.data
            });
        }).catch(error => console.log(error));
};
调用它的功能组件:

import React from 'react';

import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { getNote } from '../../redux/actions/note'

import { Typography } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(theme => ({
    container: {
        minHeight: '100vh',
        paddingTop: '25vh',
    },
}))


const Note = () => {
    const classes = useStyles();

    console.log(getNote());

    React.useEffect(() => {
        console.log('componentDidMount is useEffect in React Hooks');
        getNote();
    }, [])

    Note.propTypes = {
        note: PropTypes.array.isRequired
    }
    return (
    <div className={classes.container}>
        <Typography>
            This is note page.
        </Typography>
    </div>
);}

const mapStateToProps = state => ({
    note: state.note.note
});

export default connect(
    mapStateToProps, 
    { getNote }
)(Note);

Github代码链接:

问题在于,如果
connect
是通过
props
为您传递的,则您不调用导入的原始函数

您可以在功能组件中使用
connect
,但是
react-redux
也提供了一个可以替代
connect
HOC的

使用
连接的示例

const Note=props=>{
useEffect(props.getNote,[]))
返回。。。
}
//不要在组件主体内定义propTypes
Note.propTypes={
注意:需要PropTypes.array.isRequired
}
常量mapStateToProps=状态=>({
注:state.note.note
})
导出默认连接(
MapStateTops,
{getNote}
)(注)
使用钩子的示例

const Note=()=>{
const note=useSelector(state=>state.note.note)
const dispatch=usedpatch()
useffect(()=>{
分派(getNote())
}, [])
返回。。。
}

我认为你应该使用props.getNote()并发送它,一旦你这样做了,你就应该做得很好。

刚刚了解的connect()在钩子中没有使用,在文档中挖掘,将会更新。谢谢你,我在不知不觉中尝试合并钩子,把事情搞砸了。
note.jsx:21 dispatch => {
console.log('Contacting API');
  axios__WEBPACK_IMPORTED_MODULE_0___default.a.get('/api/note/').then(response => {
    console.log('api contact');
    dispatch({
      type: _types__W…
note.jsx:24 componentDidMount is useEffect in React Hooks