I am standing in a tricky situation.
I my reducer rhythmReducer.js is the following:
import {TOGGLE_NOTE_VALUE} from '../constants/actionTypes';
import objectAssign from 'object-assign';
import initialState from './initialState';
export default function rhythmReducer(state = initialState.rhythm, action) {
let newState = objectAssign({}, state);
console.log("---RhythmReducer");
console.log(action.type);
switch (action.type) {
case TOGGLE_NOTE_VALUE:
console.log("TOGGLE_NOTE_VALUE");
return newState;
default:
return newState;
}
}
The component using it is RhythmContainer.js:
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as actions from '../actions/rhythmActions';
import {Meter} from './Meter';
export const RhythmContainer = (props) => {
let rows = [];
for (let i=0; i < props.rhythm.meters.length; i++) {
rows.push(<Meter key={i} actions={actions} rhythm= {props.rhythm.meters[i]}/>);
}
const handleClick = () => {
return props.store.dispatch(actions.toggleNoteValue);
};
return (
<div onClick={handleClick}>
This will be a 4/4 rhythm
{rows}
</div>
);
};
RhythmContainer.propTypes = {
rhythm: PropTypes.object.isRequired,
store: PropTypes.object.isRequired,
};
function mapStateToProps(state) {
return {
rhythm: state.rhythm,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch)
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(RhythmContainer);
My action is defined in rhythmActions.js
import * as types from '../constants/actionTypes';
export function toggleNoteValue() {
console.log("toggleNoteValue");
return {type: types.TOGGLE_NOTE_VALUE};
}
Even though the reducer runs when the page is initializing I can not get it to run when I click on the div.
toggleNoteValue() is firing up but it never goes in the actual Reducer.
Any help?
PS the full project is here just in case it helps: https://github.com/ichionid/rhythmGeneratorReact/tree/master/src
Here are a couple things to try.
In your project, configureStore.js imports a rootReducer from
"../rootReducer", but there's no such module. I'm not sure if this is
just a commit issue, but it's worth checking.
The argument to dispatch should be an action. actions.toggleNoteValue
is not an action, it's a function that returns an action. Try
props.store.dispatch(actions.toggleNoteValue()) or
props.actions.toggleNoteValue() instead.
I sometimes notice this problem when reducers don't fire because they've not been put through mapDispatchToProps correctly:
// WRONG
import { action } from './actions'
// action will still fire as a function, but that's it
const Comp = ({ label }) => <button onClick={() => action()}>{label}<button>
export default connect(mapStateToProps, { action })
// RIGHT
import { action } from './actions'
// action is sent in as a prop meaning we use the connected version rather than the action directly
const Comp = ({ action, label }) => <button onClick={() => action()}>{label}<button>
export default connect(mapStateToProps, { action })
Related
I just tried make simply reducer in react redux but it never called. After a lot trial i have no idea why it's not working. console.log in action is showing but reducer never is called.
import React from "react";
import { connect } from "react-redux";
import * as actions from "store/actions";
function Login(props) {
const login = (e) => {
e.preventDefault();
props.login();
};
return (
<form onSubmit={login}>
<button> login </button>
</form>
);
}
const mapDispatchToProps = (dispatch) => {
return {
login: () => dispatch(actions.login),
};
};
export default connect(null, mapDispatchToProps)(Login);
actions file- i'm here console.log is showing correctly
import * as actionsTypes from "./actionTypes";
export const logout = () => {
return {
type: actionsTypes.AUTH_LOGOUT,
};
};
export const login = () => {
console.log("i'm here")
return {
type: actionsTypes.AUTH_LOGIN,
};
};
reducer
import * as actionTypes from "../actions/actionTypes";
const initialState = {
isLogged: false,
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.AUTH_LOGIN:
return {
...state,
isLogged: true,
};
case actionTypes.AUTH_LOGOUT:
return {
...state,
isLogged: false,
};
default:
return state;
}
};
export default reducer;
many thanks for help
Probably, you forget to make a configuration of the store itself? :)
Something like that:
// at configureStore.js
import { createStore } from 'redux';
import reducer from '../path/to/your/root/reducer'; // assuming that you use combineReducer function to gather all reducers in one place
export default createStore(reducer);
Then in your app root you need to wrap entry component with the store provider:
import store from './store/configureStore';
import { Provider } from 'react-redux';
export default () => (
<Provider store={store}>
<AppRootComponent />
</Provider>
);
AppRootComponent -> your initial app component
For reference - how to configure store
UPD:
Looks like you were trying to pass the action creator to the dispatch function, instead of invoking it actually. Just make a call of that creator in the dispatch:
login: () => dispatch(actions.login()),
BTW, here is the working example of your case
I have a simple component I'm trying to make work with redux. I map both props and dispatch actions, however only the props I initially get from the store work properly. I traced it all down to my actions: they are being dispatched, but respective reducers don't really do anything. Pretty simple stuff I came up with according to the tutorial and everything looks good to me, but I can't wrap my head around the problem here.
Here is a simplified version of the app:
// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import Search from './Search'
import { Provider } from 'react-redux'
import store from './store'
const root = document.querySelector('#app')
ReactDOM.render(
<Provider store={store}>
<Search/>
</Provider>, root)
// Search.js
import React from 'react'
import { setText } from '../../actions/appActions'
import { connect } from 'react-redux';
const mapStateToProps = state => {
return {
text: state.app.searchText
}
}
const mapDispatchToProps = dispatch => {
return {
setText,
dispatch
}
}
class Search extends React.Component {
constructor() {
super()
}
render() {
return (
<input type="text" onChange={() => this.props.setText("text")} value={this.props.text}/>
)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Search)
// store.js
import { createStore, combineReducers } from 'redux'
import app from './reducers/appReducer'
export default createStore(combineReducers({/*other non-relevant reducers*/, app}))
// appActions.js
export function setText(text) {
return {
type: "APP_SET_TEXT",
payload: text,
}
}
// appReducer.js
const initialState = {
isSearchActive: true,
searchText: "Text",
}
export default function reducer(state = initialState, action) {
switch (action.type) {
case "APP_SET_TEXT":
console.log("fart")
return {
...state,
searchText: action.payload,
}
default:
return state
}
}
What I'm trying to to is to simply make the input value change according to the redux state. I do get the text from {this.props.text}, the change handler onChange={() => this.props.setText("text")} is being dispatched, but the reducer for some reason fails to catch the action that was dispatched.
I think you should change the mapDispatchToProps variable like the following:
const mapDispatchToProps = dispatch => {
return {
setText = (text) => dispatch(setText(text)),
}
}
There are two ways to achieve this
// MODIFYING DISPATHCER
const mapDispatchToProps = dispatch => {
return {
changeText: data => dispatch(setText(data)),
}
}
or
// CONNECT
export default connect(mapStateToProps, {
setText
})(Search)
const mapDispatchToProps = dispatch => {
return {
setText,
dispatch
}
}
change to
const mapDispatchToProps = dispatch => {
return {
changeText: text => dispatch(setText(text)),
}
}
And in your component use this.props.changeText function
as most of the answers suggests you can dispatch the actions or else you can simply have mapDispatchToProps an object.
mapDispatchToProps = {
setText,
dispatch
}
Your HOC connect should take care of dispatching not need to external definition
Use bindActionCreators from redux
import { bindActionCreators } from 'redux';
const mapDispatchToProps = dispatch => {
const setText = bindActionCreators(setText, dispatch);
return setText;
}
Since you're mapping your dispatch to props like this:
const mapDispatchToProps = dispatch => {
return {
setText,
dispatch
}
}
You'll need to explicitly call dispatch in your component:
class Search extends React.Component {
constructor() {
super()
}
render() {
const {dispatch, setText} = this.props;
return (
<input type="text" onChange={() => dispatch(setText("text"))} value={this.props.text}/>
)
}
}
It is easier just to map dispatch to props like this: setText = (text) => dispatch(setText(text))
I learning redux by following some tutorial. Here I am getting an error
Object(...) is not a function
in this line
export default connect(null, { fetchPosts } )(Posts);
of the following container
import React, { Component } from 'react';
import { connect } from 'react'
import axios from 'axios';
import { fetchPosts } from '../actions/postActions.js';
class Posts extends Component {
componentWillMount() {
this.props.fetchPosts();
}
render() {
const postItems = this.state.posts.map(el => {
return (
<div key={el.id}>
<h3>{el.title} </h3>
<p>{el.body} </p>
</div>
)
})
return (
<div>
<h1> Posts </h1>
{postItems}
</div>
);
}
}
export default connect(null, { fetchPosts } )(Posts);
Ps: I know it will also throw the map error but I am not worried about it at the moment.
Since it says Object(...) is not a function and marks the line export, the only object we have inside export is the fetchPosts which looks like this (it is an action function)
import { FETCH_POST, NEW_POST } from './type.js'
import axios from 'axios';
export const fetchPosts = () => {
return function (dispatch) {
axios.get("https://jsonplaceholder.typicode.com/posts").then((response) => dispatch({
type: FETCH_POST,
payload: response
}))
}
}
Since I am not sure the relevant code for the same, the fetchPosts dispatches an acton to the following reducer
import { FETCH_POST, NEW_POST } from '../actions/type.js'
const initialState = {
items: [],
item: {}
}
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_POST:
return {
...state,
items: action.payload
}
default:
return state
}
}
Which is later combined using combineReducers in our rootreducer.js file (which is then imported to store.js where we create store)
Can Anyone tell me what I am doing wrong and How can we fix it?
React does not have a named connect export, it belongs to react-redux. So, you should import it like that:
import { connect } from "react-redux";
I am learning Redux. I am trying to connect dispatch function from container component to presentation component.
Container Component:
//FILE : app/javascript/packs/containers/registration.js
import { connect } from 'react-redux'
import { fetchCountry } from '../actions'
import Countries from '../components/registration/countries';
const getCountry = (state, filter) => {
switch (filter) {
case 'region':
console.log("Get Country Triggered",state)
default:
console.log("Get Country Default Triggered",state)
}
}
const mapStateToProps = state => ({
countries:getCountry(state,'region')
})
const mapDispatchToProps = dispatch => ({
fetchCountry: region => dispatch(fetchCountry(region))
})
export default connect(
mapStateToProps,
mapDispatchToProps
)(Countries)
Presentational Component
// FILE : /app/javascript/packs/components/registration/countries.jsx
import React from 'react'
import PropTypes from 'prop-types'
const Countries = ({ fetchCountry }) => (
<ul>
<li>
<button onClick={() => fetchCountry('region')}>Get Country</button>
</li>
</ul>
)
Countries.propTypes = {
fetchCountry: PropTypes.func.isRequired
}
Actions :
//FILE : /app/javascript/packs/actions/index.js
/* Action types */
export const FETCH_COUNTRY = "FETCH_COUNTRY";
export const FETCH_CITY = "FETCH_CITY";
/* Action creators */
export function fetchCountry(region) {
return { type: FETCH_COUNTRY, region };
}
Reducer
// FILE: /app/javascript/packs/reducers/fetchPlace.js
const fetchPlace = (state = [], action) => {
switch (action.type) {
case 'FETCH_COUNTRY':
console.log('FETCH COUNTRY Reducer');
default:
return state
}
}
export default fetchPlace
I am fetchCountry is undefined error when I try to load the page.
warning.js:33 Warning: Failed prop type: The propfetchCountryis marked as required inCountries, but its value isundefined.
I understand , i am missing some basics here, any help will be highly appreciated.
Maybe this line is your problem. Depends on if you have an index.js
import { fetchCountry } from '../actions'
should be
import { fetchCountry } from '../actions/action_file_name'
I am not able to understand why reducer is being called in one case and not in another case. The footer component is given below.
File Footer.jsx
import React from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import './Footer.scss';
import * as footerActions from '../../redux/actions/footer';
const mapStateToProps = (state) => {
return state;//{state: state, footerState: getFooterData(state)};
};
// Footer component is aware of only footer actions
const mapDispatchToProps = (dispatch) => {
return {
actions: bindActionCreators(footerActions, dispatch)
};
};
var Footer = React.createClass({
propTypes:{
state: React.PropTypes.object,
actions: React.PropTypes.object
},
componentWillMount() {
this.props.actions.changeLanguage("dutch");//<-- This calls reducer and changes language
},
changeLang(language) {
this.props.actions.changeLanguage("spanish");//<--- This doesn't calls reducer
},
render() {
var that = this;
return (
<div className="footer">
<div onClick={that.changeLang.bind(that, 'english')}>English</div>
<div onClick={that.changeLang.bind(that,'german')}>German</div>
</div>
);
}
});
export default connect(mapStateToProps, mapDispatchToProps)(Footer);
actions file footer
export const LANGUAGE_CHANGE = 'LANGUAGE_CHANGE';
export function changeLanguage(data, context) {
return {
type: LANGUAGE_CHANGE,
data
};
}
Reducer file footer
import { LANGUAGE_CHANGE } from '../actions/footer';
export default function footer(state = {}, action) {
switch (action.type) {
case LANGUAGE_CHANGE:
debugger;
return Object.assign({}, state, {language:action.data});
default:
return state;
}
}
In footer.jsx component code, dispatching LANGUAGE_CHANGE action from componentDidMount method successfully calls footer reducer but, reducer is not called when LANGUAGE_CHANGE action dispatched from changeLang method. Not able to figure out what is going wrong ? Help Appreciated.