action
export const fetchQuote = url => {
return dispatch => {
fetch(url)
.then(res => res.json())
.then(res => dispatch({ type: 'quote', data: res }))
}
}
reducer
const initialState = {
company: [],
quote: []
}
export const getChares = (state = initialState, action) => {
switch (action.type) {
case 'company':
console.log('company', action.data)
return {
...state,
company: action.data
}
case 'quote':
console.log('quote', action.data)
return {
...state,
quote: action.data
}
default:
return state
}
}
component
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchQuote } from '../../Redux/actions/actions';
import { __apiKey, __apiRealTime } from '../../__apis/__apis';
class RealTimeQuote extends Component {
componentDidMount() {
this.props.fetchQuote(__apiRealTime + __apiKey);
}
render() {
console.log('dfgvbd', this.props.quote)
const test = this.props.quote.map((res, i) => {
return (
<li key={i}>{res.volume}</li>
)
})
return (
<div>
<ul>
{test}
</ul>
</div>
)
}
}
export default connect(state => (
{
quote: state.fetchQuote.quote
}
), { fetchQuote })(RealTimeQuote)
error text:
TypeError: Cannot read property 'quote' of undefined
ругается на : {
quote: state.fetchQuote.quote
}
при этом есть еще один вызов api точно по такому же примеру и все отрабатывает