Redux not passing state to props
up vote
1
down vote
favorite
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,
},
};
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
|
show 7 more comments
up vote
1
down vote
favorite
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,
},
};
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
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 likeconst 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
|
show 7 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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,
},
};
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
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,
},
};
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
reactjs redux react-redux
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 likeconst 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
|
show 7 more comments
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 likeconst 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
|
show 7 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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