Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Redux - this.props.actions.fetchPosts is not a function

i have issue with calling async action from my component, i think i did everything what was needed to work but seems like not, i used:

mapDispatchToProps

and inside i return

actions: bindActionCreators(fetchPosts, dispatch)

and i connect it.

After all these things, i try to call this action in my component -

this.props.actions.fetchPosts()

in result i get this error in console -

this.props.actions.fetchPosts is not a function

enter image description here

And i can not understand what's the problem with it as i did everything, here will be the full source:

Component

import React, { Component } from 'react';
import { Link } from 'react-router';
import styles from './Home.css';
import { fetchPosts } from '../actions/counter';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';


    class Home extends Component {

        constructor(props) {
            super(props)
        }
        render() {
                return (
                        <div>
                            <div className="container">
                                <div className="banner_animated">
                                    <p> dadasda</p>
                                </div>
                            </div>
                            <div className="container-fluid">
                                <div className="center">
                                    <input type="text"/>
                                    <button className="btn-2 btn-2a btn" onClick={this.props.actions.fetchPosts()}>Button</button>
                                </div>
                            </div>

                        </div>
                );
        }
}
function mapStateToProps(state) {
        return state
}
function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(fetchPosts, dispatch)
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(Home);

Action

import { FETCHING_WIN_RATES, FETCHED_WIN_RATES } from '../const';
import { firebaseDb } from './firebase';

const ref = firebaseDb.ref("win_rate");

    function fetchingWinRates() {
        return {
                type: FETCHING_WIN_RATES
        };
}

    function fetchedWinRates(winRates) {
        return {
                type: FETCHED_WIN_RATES,
                winRates
        };
}
// win rate champions
export function fetchPosts() {
        return dispatch => {
                dispatch(fetchingWinRates());
                ref.on("value", function(snapshot) {
                        dispatch(fetchedWinRates(snapshot));
                        console.log(snapshot.val());
                }, function (errorObject) {
                        console.log("The read failed: " + errorObject.code);
                });
        }
}

Write if you need some more files to help me, thank you.

like image 210
Trusislv1 Avatar asked Nov 01 '16 17:11

Trusislv1


People also ask

Is fetchposts a function in React React Redux?

React Redux - this.props.actions.fetchPosts is not a function Ask Question Asked5 years, 2 months ago Active7 months ago Viewed17k times

How do I send props from React React to component?

React Props are like function arguments in JavaScript and attributes in HTML. To send props into a component, use the same syntax as HTML attributes: Example. Add a "brand" attribute to the Car element: const myelement = <Car brand="Ford" />; The component receives the argument as a props object: Example.

What happens when you pass a function as a prop?

If you pass the result of calling the function, e.g. handleClick= {handleClick ()}, then it would get invoked immediately when the page loads, which is not what we want. If you want to pass a parameter to the function that you are passing as a prop, use an inline arrow function.

What are props in JavaScript?

Props are also how you pass data from one component to another, as parameters. If you have a variable to send, and not a string as in the example above, you just put the variable name inside curly brackets:


Video Answer


2 Answers

If you pass a function to bindActionCreators, it will return a function. See the documentation for bindActionCreators here (in the Returns section): http://redux.js.org/docs/api/bindActionCreators.html.

You are effectively assigning this.props.action = fetchPosts here, meaning you would call fetchPosts like so: this.props.action().

If you want to access via this.props.actions.fetchPosts, you need to do the following:

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators({ fetchPosts }, dispatch)
    };
}

Notice the shorthand { fetchPosts } which is the same as { fetchPosts: fetchPosts }.

like image 96
Calvin Avatar answered Oct 28 '22 03:10

Calvin


You don't need to use bindActionCreators http://redux.js.org/docs/api/bindActionCreators.html

const mapDispatchToProps = dispatch => ({
    onClick: () => dispatch(fetchPosts(id))
  })
}

and then access via this.props.onClick

like image 20
Carlos Sultana Avatar answered Oct 28 '22 04:10

Carlos Sultana