React Redux: how to update vote counts in React Redux
React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered
here
The code below was designed to update a voting system. It works fine by displaying the results as the page loads.
Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.
However, when I click either Upvote por down vote button am having error below in my Reducer
Uncaught (in promise) ReferenceError: items1 is not defined.
This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST
In the backend, I have php code which returns the array data as per below.
Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?
<?php
// Update user response on a post
$return_arr= array("upvote"=>"11", "downvote"=>"7");
echo json_encode($return_arr);
exit;
?>
Here is the array return by API Call
[{"upvote":"11", "downvote":"7"}]
Here is the code
import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { vActions } from '../_actions';
class VoteApp extends React.Component {
constructor(props) {
super(props);
this.state = {
us: 0
};
}
componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
handledownvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">
<p>Vote System in React Redux</p>
{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})
<br />
<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />
</li>
)}
</ul>
}
</div>
);
}
}
function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}
const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };
Here is my Reducer
import { userConstants } from '../_constants';
export function posts1(state = {}, action) {
switch (action.type) {
case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};
case userConstants.GETALL_FAILURE:
return {
error: action.error
};
// Reducer section for Sending upvote and down vote
case userConstants.VOTE_REQUEST_POST:
return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)
};
case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {
//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };
}
//return post1;
})
};
case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};
default:
return state
}
}
reactjs redux
add a comment |
React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered
here
The code below was designed to update a voting system. It works fine by displaying the results as the page loads.
Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.
However, when I click either Upvote por down vote button am having error below in my Reducer
Uncaught (in promise) ReferenceError: items1 is not defined.
This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST
In the backend, I have php code which returns the array data as per below.
Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?
<?php
// Update user response on a post
$return_arr= array("upvote"=>"11", "downvote"=>"7");
echo json_encode($return_arr);
exit;
?>
Here is the array return by API Call
[{"upvote":"11", "downvote":"7"}]
Here is the code
import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { vActions } from '../_actions';
class VoteApp extends React.Component {
constructor(props) {
super(props);
this.state = {
us: 0
};
}
componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
handledownvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">
<p>Vote System in React Redux</p>
{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})
<br />
<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />
</li>
)}
</ul>
}
</div>
);
}
}
function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}
const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };
Here is my Reducer
import { userConstants } from '../_constants';
export function posts1(state = {}, action) {
switch (action.type) {
case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};
case userConstants.GETALL_FAILURE:
return {
error: action.error
};
// Reducer section for Sending upvote and down vote
case userConstants.VOTE_REQUEST_POST:
return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)
};
case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {
//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };
}
//return post1;
})
};
case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};
default:
return state
}
}
reactjs redux
I think you should fix the format of the codes first, they can't be read.
– Marson Mao
Dec 18 '18 at 3:47
add a comment |
React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered
here
The code below was designed to update a voting system. It works fine by displaying the results as the page loads.
Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.
However, when I click either Upvote por down vote button am having error below in my Reducer
Uncaught (in promise) ReferenceError: items1 is not defined.
This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST
In the backend, I have php code which returns the array data as per below.
Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?
<?php
// Update user response on a post
$return_arr= array("upvote"=>"11", "downvote"=>"7");
echo json_encode($return_arr);
exit;
?>
Here is the array return by API Call
[{"upvote":"11", "downvote":"7"}]
Here is the code
import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { vActions } from '../_actions';
class VoteApp extends React.Component {
constructor(props) {
super(props);
this.state = {
us: 0
};
}
componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
handledownvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">
<p>Vote System in React Redux</p>
{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})
<br />
<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />
</li>
)}
</ul>
}
</div>
);
}
}
function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}
const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };
Here is my Reducer
import { userConstants } from '../_constants';
export function posts1(state = {}, action) {
switch (action.type) {
case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};
case userConstants.GETALL_FAILURE:
return {
error: action.error
};
// Reducer section for Sending upvote and down vote
case userConstants.VOTE_REQUEST_POST:
return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)
};
case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {
//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };
}
//return post1;
})
};
case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};
default:
return state
}
}
reactjs redux
React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered
here
The code below was designed to update a voting system. It works fine by displaying the results as the page loads.
Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.
However, when I click either Upvote por down vote button am having error below in my Reducer
Uncaught (in promise) ReferenceError: items1 is not defined.
This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST
In the backend, I have php code which returns the array data as per below.
Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?
<?php
// Update user response on a post
$return_arr= array("upvote"=>"11", "downvote"=>"7");
echo json_encode($return_arr);
exit;
?>
Here is the array return by API Call
[{"upvote":"11", "downvote":"7"}]
Here is the code
import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { vActions } from '../_actions';
class VoteApp extends React.Component {
constructor(props) {
super(props);
this.state = {
us: 0
};
}
componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
handledownvote(person_id,person_vote) {
return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));
}
render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">
<p>Vote System in React Redux</p>
{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})
<br />
<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />
</li>
)}
</ul>
}
</div>
);
}
}
function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}
const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };
Here is my Reducer
import { userConstants } from '../_constants';
export function posts1(state = {}, action) {
switch (action.type) {
case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};
case userConstants.GETALL_FAILURE:
return {
error: action.error
};
// Reducer section for Sending upvote and down vote
case userConstants.VOTE_REQUEST_POST:
return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)
};
case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {
//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };
}
//return post1;
})
};
case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};
default:
return state
}
}
reactjs redux
reactjs redux
edited Dec 18 '18 at 20:20
jmarkatti
asked Dec 18 '18 at 2:42
jmarkattijmarkatti
15219
15219
I think you should fix the format of the codes first, they can't be read.
– Marson Mao
Dec 18 '18 at 3:47
add a comment |
I think you should fix the format of the codes first, they can't be read.
– Marson Mao
Dec 18 '18 at 3:47
I think you should fix the format of the codes first, they can't be read.
– Marson Mao
Dec 18 '18 at 3:47
I think you should fix the format of the codes first, they can't be read.
– Marson Mao
Dec 18 '18 at 3:47
add a comment |
1 Answer
1
active
oldest
votes
This is what fix my problem. Since the json data returned is in array.
items1: action.posts1[0].upvote
Thanks
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53825713%2freact-redux-how-to-update-vote-counts-in-react-redux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is what fix my problem. Since the json data returned is in array.
items1: action.posts1[0].upvote
Thanks
add a comment |
This is what fix my problem. Since the json data returned is in array.
items1: action.posts1[0].upvote
Thanks
add a comment |
This is what fix my problem. Since the json data returned is in array.
items1: action.posts1[0].upvote
Thanks
This is what fix my problem. Since the json data returned is in array.
items1: action.posts1[0].upvote
Thanks
answered Dec 31 '18 at 11:58
jmarkattijmarkatti
15219
15219
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53825713%2freact-redux-how-to-update-vote-counts-in-react-redux%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
I think you should fix the format of the codes first, they can't be read.
– Marson Mao
Dec 18 '18 at 3:47