Redux - Updating A Component With 3rd Party API
up vote
0
down vote
favorite
I know I'm close but can't figure out what else needs to be done here.
I have an action that displays all of the data I need, I am having trouble getting this data over to my component:
This is the action:
export const wishListActions = username => dispatch => {
fetch(`${API_BASE_URL}/Wishlist/User/${username}`)
.then(res => res.json())
.then(data => {
(data.map(i => {
dispatch(findWishListSuccess(i.wishlists));
}));
})
.catch(error => console.log(error))
}
export const FIND_WISHLIST_BEGIN = 'FIND_WISHLIST_BEGIN'
export const findWishListBegin = wishlist => ({
type: FIND_WISHLIST_BEGIN,
wishlist
});
export const FIND_WISHLIST_SUCCESS = 'FIND_WISHLIST_SUCCESS'
export const findWishListSuccess = wishlist => ({
type: FIND_WISHLIST_SUCCESS,
wishlist
});
export const FIND_WISHLIST_FAILURE = 'FIND_WISHLIST_FAILURE'
export const findWishListFailure = wishlist => ({
type: FIND_WISHLIST_FAILURE,
wishlist
});
This is the reducer:
const initialState = {
//state.products.items is here
wishlist: [{
text: "Category - Christmas",
items: [
{
name: "Fridge",
salePrice: "200",
image: "pictureurl.com",
productUrl: "google.com"
}
]
}],
loading: false,
error: null
};
export default function wishlistReducer(state = initialState, action) {
switch (action.type) {
case FIND_WISHLIST_BEGIN:
return {
...state,
loading: true,
error: null
};
case FIND_WISHLIST_SUCCESS:
console.log(action.wishlist.items);
return Object.assign({}, state, {
wishlist: [
...state.wishlist, {
items: action.wishlist
}
],
loading: false
});
This is the component:
export class Wishlist extends React.Component{
componentDidMount(){
if(window.localStorage.userName){
this.props.dispatch(wishListActions(window.localStorage.userName));
}
}
render(){
const userWishlist = this.props.wishlist.map(i => (
<div key={i.peoductUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
));
return(
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
)
}
}
Wishlist.defaultProps= {
title: "My Wishlist",
wishlist: [{
text:"Presents",
name:"VIZIO 4K TV",
salePrice:"200",
}]
}
const mapStateToProps = state => ({
wishlist: state.wishlist
})
export default connect(mapStateToProps)(Wishlist);
When I click on my redux chrome extension tool, it's showing multiple wishlists as such, but my component is still showing the "initial state" which has the Vizio 4k tv I set in the component. I've check existing questions and have gone to at least 3 Q&A sessions at my bootcamp with no success, so I apologize ahead of time if this is a "repeat post."
reactjs api redux action
add a comment |
up vote
0
down vote
favorite
I know I'm close but can't figure out what else needs to be done here.
I have an action that displays all of the data I need, I am having trouble getting this data over to my component:
This is the action:
export const wishListActions = username => dispatch => {
fetch(`${API_BASE_URL}/Wishlist/User/${username}`)
.then(res => res.json())
.then(data => {
(data.map(i => {
dispatch(findWishListSuccess(i.wishlists));
}));
})
.catch(error => console.log(error))
}
export const FIND_WISHLIST_BEGIN = 'FIND_WISHLIST_BEGIN'
export const findWishListBegin = wishlist => ({
type: FIND_WISHLIST_BEGIN,
wishlist
});
export const FIND_WISHLIST_SUCCESS = 'FIND_WISHLIST_SUCCESS'
export const findWishListSuccess = wishlist => ({
type: FIND_WISHLIST_SUCCESS,
wishlist
});
export const FIND_WISHLIST_FAILURE = 'FIND_WISHLIST_FAILURE'
export const findWishListFailure = wishlist => ({
type: FIND_WISHLIST_FAILURE,
wishlist
});
This is the reducer:
const initialState = {
//state.products.items is here
wishlist: [{
text: "Category - Christmas",
items: [
{
name: "Fridge",
salePrice: "200",
image: "pictureurl.com",
productUrl: "google.com"
}
]
}],
loading: false,
error: null
};
export default function wishlistReducer(state = initialState, action) {
switch (action.type) {
case FIND_WISHLIST_BEGIN:
return {
...state,
loading: true,
error: null
};
case FIND_WISHLIST_SUCCESS:
console.log(action.wishlist.items);
return Object.assign({}, state, {
wishlist: [
...state.wishlist, {
items: action.wishlist
}
],
loading: false
});
This is the component:
export class Wishlist extends React.Component{
componentDidMount(){
if(window.localStorage.userName){
this.props.dispatch(wishListActions(window.localStorage.userName));
}
}
render(){
const userWishlist = this.props.wishlist.map(i => (
<div key={i.peoductUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
));
return(
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
)
}
}
Wishlist.defaultProps= {
title: "My Wishlist",
wishlist: [{
text:"Presents",
name:"VIZIO 4K TV",
salePrice:"200",
}]
}
const mapStateToProps = state => ({
wishlist: state.wishlist
})
export default connect(mapStateToProps)(Wishlist);
When I click on my redux chrome extension tool, it's showing multiple wishlists as such, but my component is still showing the "initial state" which has the Vizio 4k tv I set in the component. I've check existing questions and have gone to at least 3 Q&A sessions at my bootcamp with no success, so I apologize ahead of time if this is a "repeat post."
reactjs api redux action
Are you including other state in your initialState object in your reducer?
– Goose
Nov 19 at 1:15
Another issue: seems like your wishlist objects are different in the intialState object in your reducer and the one in your component.
– Goose
Nov 19 at 1:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I know I'm close but can't figure out what else needs to be done here.
I have an action that displays all of the data I need, I am having trouble getting this data over to my component:
This is the action:
export const wishListActions = username => dispatch => {
fetch(`${API_BASE_URL}/Wishlist/User/${username}`)
.then(res => res.json())
.then(data => {
(data.map(i => {
dispatch(findWishListSuccess(i.wishlists));
}));
})
.catch(error => console.log(error))
}
export const FIND_WISHLIST_BEGIN = 'FIND_WISHLIST_BEGIN'
export const findWishListBegin = wishlist => ({
type: FIND_WISHLIST_BEGIN,
wishlist
});
export const FIND_WISHLIST_SUCCESS = 'FIND_WISHLIST_SUCCESS'
export const findWishListSuccess = wishlist => ({
type: FIND_WISHLIST_SUCCESS,
wishlist
});
export const FIND_WISHLIST_FAILURE = 'FIND_WISHLIST_FAILURE'
export const findWishListFailure = wishlist => ({
type: FIND_WISHLIST_FAILURE,
wishlist
});
This is the reducer:
const initialState = {
//state.products.items is here
wishlist: [{
text: "Category - Christmas",
items: [
{
name: "Fridge",
salePrice: "200",
image: "pictureurl.com",
productUrl: "google.com"
}
]
}],
loading: false,
error: null
};
export default function wishlistReducer(state = initialState, action) {
switch (action.type) {
case FIND_WISHLIST_BEGIN:
return {
...state,
loading: true,
error: null
};
case FIND_WISHLIST_SUCCESS:
console.log(action.wishlist.items);
return Object.assign({}, state, {
wishlist: [
...state.wishlist, {
items: action.wishlist
}
],
loading: false
});
This is the component:
export class Wishlist extends React.Component{
componentDidMount(){
if(window.localStorage.userName){
this.props.dispatch(wishListActions(window.localStorage.userName));
}
}
render(){
const userWishlist = this.props.wishlist.map(i => (
<div key={i.peoductUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
));
return(
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
)
}
}
Wishlist.defaultProps= {
title: "My Wishlist",
wishlist: [{
text:"Presents",
name:"VIZIO 4K TV",
salePrice:"200",
}]
}
const mapStateToProps = state => ({
wishlist: state.wishlist
})
export default connect(mapStateToProps)(Wishlist);
When I click on my redux chrome extension tool, it's showing multiple wishlists as such, but my component is still showing the "initial state" which has the Vizio 4k tv I set in the component. I've check existing questions and have gone to at least 3 Q&A sessions at my bootcamp with no success, so I apologize ahead of time if this is a "repeat post."
reactjs api redux action
I know I'm close but can't figure out what else needs to be done here.
I have an action that displays all of the data I need, I am having trouble getting this data over to my component:
This is the action:
export const wishListActions = username => dispatch => {
fetch(`${API_BASE_URL}/Wishlist/User/${username}`)
.then(res => res.json())
.then(data => {
(data.map(i => {
dispatch(findWishListSuccess(i.wishlists));
}));
})
.catch(error => console.log(error))
}
export const FIND_WISHLIST_BEGIN = 'FIND_WISHLIST_BEGIN'
export const findWishListBegin = wishlist => ({
type: FIND_WISHLIST_BEGIN,
wishlist
});
export const FIND_WISHLIST_SUCCESS = 'FIND_WISHLIST_SUCCESS'
export const findWishListSuccess = wishlist => ({
type: FIND_WISHLIST_SUCCESS,
wishlist
});
export const FIND_WISHLIST_FAILURE = 'FIND_WISHLIST_FAILURE'
export const findWishListFailure = wishlist => ({
type: FIND_WISHLIST_FAILURE,
wishlist
});
This is the reducer:
const initialState = {
//state.products.items is here
wishlist: [{
text: "Category - Christmas",
items: [
{
name: "Fridge",
salePrice: "200",
image: "pictureurl.com",
productUrl: "google.com"
}
]
}],
loading: false,
error: null
};
export default function wishlistReducer(state = initialState, action) {
switch (action.type) {
case FIND_WISHLIST_BEGIN:
return {
...state,
loading: true,
error: null
};
case FIND_WISHLIST_SUCCESS:
console.log(action.wishlist.items);
return Object.assign({}, state, {
wishlist: [
...state.wishlist, {
items: action.wishlist
}
],
loading: false
});
This is the component:
export class Wishlist extends React.Component{
componentDidMount(){
if(window.localStorage.userName){
this.props.dispatch(wishListActions(window.localStorage.userName));
}
}
render(){
const userWishlist = this.props.wishlist.map(i => (
<div key={i.peoductUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
));
return(
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
)
}
}
Wishlist.defaultProps= {
title: "My Wishlist",
wishlist: [{
text:"Presents",
name:"VIZIO 4K TV",
salePrice:"200",
}]
}
const mapStateToProps = state => ({
wishlist: state.wishlist
})
export default connect(mapStateToProps)(Wishlist);
When I click on my redux chrome extension tool, it's showing multiple wishlists as such, but my component is still showing the "initial state" which has the Vizio 4k tv I set in the component. I've check existing questions and have gone to at least 3 Q&A sessions at my bootcamp with no success, so I apologize ahead of time if this is a "repeat post."
reactjs api redux action
reactjs api redux action
asked Nov 19 at 0:44
JVega
51
51
Are you including other state in your initialState object in your reducer?
– Goose
Nov 19 at 1:15
Another issue: seems like your wishlist objects are different in the intialState object in your reducer and the one in your component.
– Goose
Nov 19 at 1:18
add a comment |
Are you including other state in your initialState object in your reducer?
– Goose
Nov 19 at 1:15
Another issue: seems like your wishlist objects are different in the intialState object in your reducer and the one in your component.
– Goose
Nov 19 at 1:18
Are you including other state in your initialState object in your reducer?
– Goose
Nov 19 at 1:15
Are you including other state in your initialState object in your reducer?
– Goose
Nov 19 at 1:15
Another issue: seems like your wishlist objects are different in the intialState object in your reducer and the one in your component.
– Goose
Nov 19 at 1:18
Another issue: seems like your wishlist objects are different in the intialState object in your reducer and the one in your component.
– Goose
Nov 19 at 1:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
There is a difference in the state structure being built by wishlistReducer
and what is being expected by the Wishlist
component. The Wishlist
component is expecting an array of the form:
[
{ text: "", name: "", salesPrice: "" },
]
while state.wishlist
is of the following form:
[
{
items: [
{ name: "", salesPrice: "", image: "", productUrl: "" }
]
}
]
So it looks like your Wishlist
component is actually expecting an item, rather than an entire wishlist. Your Wishlist
component should probably look similar to the example below:
export class Wishlist extends React.Component{
render(){
const userWishlist = this.props.wishlist.map(w => (
<div key={w.name}>
<h3>{w.name}</h3>
{w.items.map(i => (
<div key={i.productUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
)}
</div>
));
return (
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
);
}
}
Though at this point it is more of a list of wishlists, but hopefully this demonstrates how the state structure is being passed into your Wishlist
component.
Also, this is assuming that wishlistReducer
is your root reducers. If you are using combineReducers
, the state structure will be another level deeper, such as:
combineReducers({
wishlist: wishlistReducer,
});
const mapStateToProps = state => ({
wishlist: state.wishlist.wishlist
})
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
There is a difference in the state structure being built by wishlistReducer
and what is being expected by the Wishlist
component. The Wishlist
component is expecting an array of the form:
[
{ text: "", name: "", salesPrice: "" },
]
while state.wishlist
is of the following form:
[
{
items: [
{ name: "", salesPrice: "", image: "", productUrl: "" }
]
}
]
So it looks like your Wishlist
component is actually expecting an item, rather than an entire wishlist. Your Wishlist
component should probably look similar to the example below:
export class Wishlist extends React.Component{
render(){
const userWishlist = this.props.wishlist.map(w => (
<div key={w.name}>
<h3>{w.name}</h3>
{w.items.map(i => (
<div key={i.productUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
)}
</div>
));
return (
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
);
}
}
Though at this point it is more of a list of wishlists, but hopefully this demonstrates how the state structure is being passed into your Wishlist
component.
Also, this is assuming that wishlistReducer
is your root reducers. If you are using combineReducers
, the state structure will be another level deeper, such as:
combineReducers({
wishlist: wishlistReducer,
});
const mapStateToProps = state => ({
wishlist: state.wishlist.wishlist
})
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
add a comment |
up vote
0
down vote
accepted
There is a difference in the state structure being built by wishlistReducer
and what is being expected by the Wishlist
component. The Wishlist
component is expecting an array of the form:
[
{ text: "", name: "", salesPrice: "" },
]
while state.wishlist
is of the following form:
[
{
items: [
{ name: "", salesPrice: "", image: "", productUrl: "" }
]
}
]
So it looks like your Wishlist
component is actually expecting an item, rather than an entire wishlist. Your Wishlist
component should probably look similar to the example below:
export class Wishlist extends React.Component{
render(){
const userWishlist = this.props.wishlist.map(w => (
<div key={w.name}>
<h3>{w.name}</h3>
{w.items.map(i => (
<div key={i.productUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
)}
</div>
));
return (
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
);
}
}
Though at this point it is more of a list of wishlists, but hopefully this demonstrates how the state structure is being passed into your Wishlist
component.
Also, this is assuming that wishlistReducer
is your root reducers. If you are using combineReducers
, the state structure will be another level deeper, such as:
combineReducers({
wishlist: wishlistReducer,
});
const mapStateToProps = state => ({
wishlist: state.wishlist.wishlist
})
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
There is a difference in the state structure being built by wishlistReducer
and what is being expected by the Wishlist
component. The Wishlist
component is expecting an array of the form:
[
{ text: "", name: "", salesPrice: "" },
]
while state.wishlist
is of the following form:
[
{
items: [
{ name: "", salesPrice: "", image: "", productUrl: "" }
]
}
]
So it looks like your Wishlist
component is actually expecting an item, rather than an entire wishlist. Your Wishlist
component should probably look similar to the example below:
export class Wishlist extends React.Component{
render(){
const userWishlist = this.props.wishlist.map(w => (
<div key={w.name}>
<h3>{w.name}</h3>
{w.items.map(i => (
<div key={i.productUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
)}
</div>
));
return (
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
);
}
}
Though at this point it is more of a list of wishlists, but hopefully this demonstrates how the state structure is being passed into your Wishlist
component.
Also, this is assuming that wishlistReducer
is your root reducers. If you are using combineReducers
, the state structure will be another level deeper, such as:
combineReducers({
wishlist: wishlistReducer,
});
const mapStateToProps = state => ({
wishlist: state.wishlist.wishlist
})
There is a difference in the state structure being built by wishlistReducer
and what is being expected by the Wishlist
component. The Wishlist
component is expecting an array of the form:
[
{ text: "", name: "", salesPrice: "" },
]
while state.wishlist
is of the following form:
[
{
items: [
{ name: "", salesPrice: "", image: "", productUrl: "" }
]
}
]
So it looks like your Wishlist
component is actually expecting an item, rather than an entire wishlist. Your Wishlist
component should probably look similar to the example below:
export class Wishlist extends React.Component{
render(){
const userWishlist = this.props.wishlist.map(w => (
<div key={w.name}>
<h3>{w.name}</h3>
{w.items.map(i => (
<div key={i.productUrl + i.name}>
{console.log(i)}
<div className="category">{i.text}</div>
<div className="name">{i.name}</div>
<div className="price">$ {i.salePrice}</div>
<img className="image" src={i.image} alt={i.name}/>
<a className="listing-url" href={i.productUrl}>Buy Now!</a>
</div>
)}
</div>
));
return (
<div className='user-wishlist'>
<h3>{this.props.title}</h3>
{ userWishlist }
</div>
);
}
}
Though at this point it is more of a list of wishlists, but hopefully this demonstrates how the state structure is being passed into your Wishlist
component.
Also, this is assuming that wishlistReducer
is your root reducers. If you are using combineReducers
, the state structure will be another level deeper, such as:
combineReducers({
wishlist: wishlistReducer,
});
const mapStateToProps = state => ({
wishlist: state.wishlist.wishlist
})
answered Nov 19 at 1:20
Tyler
8717
8717
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
add a comment |
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
Thank you Tyler, calling getState in my component and changing my mapStateToProps function to your answer lead me to being able to update my component. Thank you!
– JVega
Nov 19 at 3:17
add a comment |
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%2f53366962%2fredux-updating-a-component-with-3rd-party-api%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
Are you including other state in your initialState object in your reducer?
– Goose
Nov 19 at 1:15
Another issue: seems like your wishlist objects are different in the intialState object in your reducer and the one in your component.
– Goose
Nov 19 at 1:18