Redux not passing state to props











up vote
1
down vote

favorite
1












I connected a component to the store in a way that it's able to dispatch actions and receive state. The dispatch works and I can see the state change in the Redux dev tool. However, I'm unable to pass the state back to the component. What am I missing here?



Component



The toggle() and addTask() are successfully dispatched. However, the newTask doesn't receive the state.



class AddTask extends React.Component {
state = {
value: '',
};

setValue = value => this.setState({ value });

handleInput = () => !this.props.newTask ? 'hidden' : '';
handleToggle = () => this.props.toggle();

handleSubmit = (e) => {
e.preventDefault();
const title = this.state.value;
this.props.addTask(title);
this.props.toggle();
};

render() {
return (
<div className="add-task">
<div className="btn-add-task">
<Button
type="LINK"
label="Add a Task"
onClick={this.handleToggle}
/>
</div>
<form
className={`task-input ${this.handleInput()}`}
onSubmit={this.handleSubmit}
>
<InputField
placeholder="Task title"
value={value => this.setValue(value)}
/>
</form>
</div>
);
}
};

export default AddTask;


Container



import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import AddTask from './TaskList/AddTask/AddTask';

import * as actionCreators from '../actions/taskActions';

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

const mapDispatchToProps = dispatch => (
bindActionCreators(actionCreators, dispatch)
);

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


Reducer



import {
TOGGLE_TASK,
} from '../constants/actions';

const uiReducer = (state = {
newTask: false,
}, action) => {
switch (action.type) {
case TOGGLE_TASK:
return {
...state,
newTask: !state.newTask,
};
default:
return state;
}
};

export default uiReducer;


Store



const initialState = {
tasks: [
{
title: 'Title A', description: 'Description A', effort: '12 hrs', editing: 'false',
},
{
title: 'Title B', description: 'Description B', effort: '24 hrs', editing: 'false',
},
{
title: 'Title C', description: 'Description C', effort: '', editing: 'false',
},
],
ui: {
newTask: false,
},
};


Print



tasks: Array(4)
0: {title: "Title A", description: "Description A", effort: "12 hrs", editing: "false"}
1: {title: "Title B", description: "Description B", effort: "24 hrs", editing: "false"}
2: {title: "Title C", description: "Description C", effort: "", editing: "false"}
3: {id: "", title: "asdfasd", description: undefined}
length: 4
__proto__: Array(0)
ui:
newTask: false


Thanks!










share|improve this question




















  • 1




    Could you post how your reducer looks like? There might be something wrong with it. Thanks :)
    – Denis Cappelini
    Nov 13 at 11:49






  • 2




    First thing, are you using the container and not the component in your app. Second thing if you use combineReducers in your app, state.newTask might not be the correct value you are trying to access. It would instead be nested inside another object which is the name of your reducer
    – Shubham Khatri
    Nov 13 at 11:52










  • @devDesigner you should use newTask like const mapStateToProps = state => ({ newTask: state.ui.newTask, });
    – Shubham Khatri
    Nov 13 at 12:09










  • Thanks for the quick reply! I added the reducer and the initial state, which matches the actual state as shown in the dev tools.
    – devDesigner
    Nov 13 at 12:09










  • @ShubhamKhatri I already tried, but it didn't work
    – devDesigner
    Nov 13 at 12:10















up vote
1
down vote

favorite
1












I connected a component to the store in a way that it's able to dispatch actions and receive state. The dispatch works and I can see the state change in the Redux dev tool. However, I'm unable to pass the state back to the component. What am I missing here?



Component



The toggle() and addTask() are successfully dispatched. However, the newTask doesn't receive the state.



class AddTask extends React.Component {
state = {
value: '',
};

setValue = value => this.setState({ value });

handleInput = () => !this.props.newTask ? 'hidden' : '';
handleToggle = () => this.props.toggle();

handleSubmit = (e) => {
e.preventDefault();
const title = this.state.value;
this.props.addTask(title);
this.props.toggle();
};

render() {
return (
<div className="add-task">
<div className="btn-add-task">
<Button
type="LINK"
label="Add a Task"
onClick={this.handleToggle}
/>
</div>
<form
className={`task-input ${this.handleInput()}`}
onSubmit={this.handleSubmit}
>
<InputField
placeholder="Task title"
value={value => this.setValue(value)}
/>
</form>
</div>
);
}
};

export default AddTask;


Container



import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import AddTask from './TaskList/AddTask/AddTask';

import * as actionCreators from '../actions/taskActions';

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

const mapDispatchToProps = dispatch => (
bindActionCreators(actionCreators, dispatch)
);

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


Reducer



import {
TOGGLE_TASK,
} from '../constants/actions';

const uiReducer = (state = {
newTask: false,
}, action) => {
switch (action.type) {
case TOGGLE_TASK:
return {
...state,
newTask: !state.newTask,
};
default:
return state;
}
};

export default uiReducer;


Store



const initialState = {
tasks: [
{
title: 'Title A', description: 'Description A', effort: '12 hrs', editing: 'false',
},
{
title: 'Title B', description: 'Description B', effort: '24 hrs', editing: 'false',
},
{
title: 'Title C', description: 'Description C', effort: '', editing: 'false',
},
],
ui: {
newTask: false,
},
};


Print



tasks: Array(4)
0: {title: "Title A", description: "Description A", effort: "12 hrs", editing: "false"}
1: {title: "Title B", description: "Description B", effort: "24 hrs", editing: "false"}
2: {title: "Title C", description: "Description C", effort: "", editing: "false"}
3: {id: "", title: "asdfasd", description: undefined}
length: 4
__proto__: Array(0)
ui:
newTask: false


Thanks!










share|improve this question




















  • 1




    Could you post how your reducer looks like? There might be something wrong with it. Thanks :)
    – Denis Cappelini
    Nov 13 at 11:49






  • 2




    First thing, are you using the container and not the component in your app. Second thing if you use combineReducers in your app, state.newTask might not be the correct value you are trying to access. It would instead be nested inside another object which is the name of your reducer
    – Shubham Khatri
    Nov 13 at 11:52










  • @devDesigner you should use newTask like const mapStateToProps = state => ({ newTask: state.ui.newTask, });
    – Shubham Khatri
    Nov 13 at 12:09










  • Thanks for the quick reply! I added the reducer and the initial state, which matches the actual state as shown in the dev tools.
    – devDesigner
    Nov 13 at 12:09










  • @ShubhamKhatri I already tried, but it didn't work
    – devDesigner
    Nov 13 at 12:10













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I connected a component to the store in a way that it's able to dispatch actions and receive state. The dispatch works and I can see the state change in the Redux dev tool. However, I'm unable to pass the state back to the component. What am I missing here?



Component



The toggle() and addTask() are successfully dispatched. However, the newTask doesn't receive the state.



class AddTask extends React.Component {
state = {
value: '',
};

setValue = value => this.setState({ value });

handleInput = () => !this.props.newTask ? 'hidden' : '';
handleToggle = () => this.props.toggle();

handleSubmit = (e) => {
e.preventDefault();
const title = this.state.value;
this.props.addTask(title);
this.props.toggle();
};

render() {
return (
<div className="add-task">
<div className="btn-add-task">
<Button
type="LINK"
label="Add a Task"
onClick={this.handleToggle}
/>
</div>
<form
className={`task-input ${this.handleInput()}`}
onSubmit={this.handleSubmit}
>
<InputField
placeholder="Task title"
value={value => this.setValue(value)}
/>
</form>
</div>
);
}
};

export default AddTask;


Container



import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import AddTask from './TaskList/AddTask/AddTask';

import * as actionCreators from '../actions/taskActions';

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

const mapDispatchToProps = dispatch => (
bindActionCreators(actionCreators, dispatch)
);

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


Reducer



import {
TOGGLE_TASK,
} from '../constants/actions';

const uiReducer = (state = {
newTask: false,
}, action) => {
switch (action.type) {
case TOGGLE_TASK:
return {
...state,
newTask: !state.newTask,
};
default:
return state;
}
};

export default uiReducer;


Store



const initialState = {
tasks: [
{
title: 'Title A', description: 'Description A', effort: '12 hrs', editing: 'false',
},
{
title: 'Title B', description: 'Description B', effort: '24 hrs', editing: 'false',
},
{
title: 'Title C', description: 'Description C', effort: '', editing: 'false',
},
],
ui: {
newTask: false,
},
};


Print



tasks: Array(4)
0: {title: "Title A", description: "Description A", effort: "12 hrs", editing: "false"}
1: {title: "Title B", description: "Description B", effort: "24 hrs", editing: "false"}
2: {title: "Title C", description: "Description C", effort: "", editing: "false"}
3: {id: "", title: "asdfasd", description: undefined}
length: 4
__proto__: Array(0)
ui:
newTask: false


Thanks!










share|improve this question















I connected a component to the store in a way that it's able to dispatch actions and receive state. The dispatch works and I can see the state change in the Redux dev tool. However, I'm unable to pass the state back to the component. What am I missing here?



Component



The toggle() and addTask() are successfully dispatched. However, the newTask doesn't receive the state.



class AddTask extends React.Component {
state = {
value: '',
};

setValue = value => this.setState({ value });

handleInput = () => !this.props.newTask ? 'hidden' : '';
handleToggle = () => this.props.toggle();

handleSubmit = (e) => {
e.preventDefault();
const title = this.state.value;
this.props.addTask(title);
this.props.toggle();
};

render() {
return (
<div className="add-task">
<div className="btn-add-task">
<Button
type="LINK"
label="Add a Task"
onClick={this.handleToggle}
/>
</div>
<form
className={`task-input ${this.handleInput()}`}
onSubmit={this.handleSubmit}
>
<InputField
placeholder="Task title"
value={value => this.setValue(value)}
/>
</form>
</div>
);
}
};

export default AddTask;


Container



import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import AddTask from './TaskList/AddTask/AddTask';

import * as actionCreators from '../actions/taskActions';

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

const mapDispatchToProps = dispatch => (
bindActionCreators(actionCreators, dispatch)
);

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


Reducer



import {
TOGGLE_TASK,
} from '../constants/actions';

const uiReducer = (state = {
newTask: false,
}, action) => {
switch (action.type) {
case TOGGLE_TASK:
return {
...state,
newTask: !state.newTask,
};
default:
return state;
}
};

export default uiReducer;


Store



const initialState = {
tasks: [
{
title: 'Title A', description: 'Description A', effort: '12 hrs', editing: 'false',
},
{
title: 'Title B', description: 'Description B', effort: '24 hrs', editing: 'false',
},
{
title: 'Title C', description: 'Description C', effort: '', editing: 'false',
},
],
ui: {
newTask: false,
},
};


Print



tasks: Array(4)
0: {title: "Title A", description: "Description A", effort: "12 hrs", editing: "false"}
1: {title: "Title B", description: "Description B", effort: "24 hrs", editing: "false"}
2: {title: "Title C", description: "Description C", effort: "", editing: "false"}
3: {id: "", title: "asdfasd", description: undefined}
length: 4
__proto__: Array(0)
ui:
newTask: false


Thanks!







reactjs redux react-redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 13:02









Spara

1,32911122




1,32911122










asked Nov 13 at 11:46









devDesigner

85




85








  • 1




    Could you post how your reducer looks like? There might be something wrong with it. Thanks :)
    – Denis Cappelini
    Nov 13 at 11:49






  • 2




    First thing, are you using the container and not the component in your app. Second thing if you use combineReducers in your app, state.newTask might not be the correct value you are trying to access. It would instead be nested inside another object which is the name of your reducer
    – Shubham Khatri
    Nov 13 at 11:52










  • @devDesigner you should use newTask like const mapStateToProps = state => ({ newTask: state.ui.newTask, });
    – Shubham Khatri
    Nov 13 at 12:09










  • Thanks for the quick reply! I added the reducer and the initial state, which matches the actual state as shown in the dev tools.
    – devDesigner
    Nov 13 at 12:09










  • @ShubhamKhatri I already tried, but it didn't work
    – devDesigner
    Nov 13 at 12:10














  • 1




    Could you post how your reducer looks like? There might be something wrong with it. Thanks :)
    – Denis Cappelini
    Nov 13 at 11:49






  • 2




    First thing, are you using the container and not the component in your app. Second thing if you use combineReducers in your app, state.newTask might not be the correct value you are trying to access. It would instead be nested inside another object which is the name of your reducer
    – Shubham Khatri
    Nov 13 at 11:52










  • @devDesigner you should use newTask like const mapStateToProps = state => ({ newTask: state.ui.newTask, });
    – Shubham Khatri
    Nov 13 at 12:09










  • Thanks for the quick reply! I added the reducer and the initial state, which matches the actual state as shown in the dev tools.
    – devDesigner
    Nov 13 at 12:09










  • @ShubhamKhatri I already tried, but it didn't work
    – devDesigner
    Nov 13 at 12:10








1




1




Could you post how your reducer looks like? There might be something wrong with it. Thanks :)
– Denis Cappelini
Nov 13 at 11:49




Could you post how your reducer looks like? There might be something wrong with it. Thanks :)
– Denis Cappelini
Nov 13 at 11:49




2




2




First thing, are you using the container and not the component in your app. Second thing if you use combineReducers in your app, state.newTask might not be the correct value you are trying to access. It would instead be nested inside another object which is the name of your reducer
– Shubham Khatri
Nov 13 at 11:52




First thing, are you using the container and not the component in your app. Second thing if you use combineReducers in your app, state.newTask might not be the correct value you are trying to access. It would instead be nested inside another object which is the name of your reducer
– Shubham Khatri
Nov 13 at 11:52












@devDesigner you should use newTask like const mapStateToProps = state => ({ newTask: state.ui.newTask, });
– Shubham Khatri
Nov 13 at 12:09




@devDesigner you should use newTask like const mapStateToProps = state => ({ newTask: state.ui.newTask, });
– Shubham Khatri
Nov 13 at 12:09












Thanks for the quick reply! I added the reducer and the initial state, which matches the actual state as shown in the dev tools.
– devDesigner
Nov 13 at 12:09




Thanks for the quick reply! I added the reducer and the initial state, which matches the actual state as shown in the dev tools.
– devDesigner
Nov 13 at 12:09












@ShubhamKhatri I already tried, but it didn't work
– devDesigner
Nov 13 at 12:10




@ShubhamKhatri I already tried, but it didn't work
– devDesigner
Nov 13 at 12:10

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280366%2fredux-not-passing-state-to-props%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280366%2fredux-not-passing-state-to-props%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?