[英]React native + redux + empty object
I have a problem with redux on my react native app. 我在本机应用程序上的redux出现问题。 I fetch an API, in my action controller whan i console.log the data i have my object of data.
我在我的动作控制器中通过console.log获取数据,但有一个数据对象。 In my reducer when i console.log action.data i have my object data.
在我的reducer中,当我console.log action.data时,我有我的对象数据。 However when i'm in my component and try console.log(state.myreducer) in mapStateToProps i have something like this [Object Object].
但是,当我在组件中并在mapStateToProps中尝试console.log(state.myreducer)时,我会遇到类似[Object Object]的问题。 I tried to map on the data but i have an error (undefined).
我试图映射数据,但出现错误(未定义)。 I don't know where is the problem.
我不知道问题出在哪里。 Here after my code
在我的代码之后
action.js action.js
export function getConditions(lat, long){
const url = `${URL_CONDITIONS}${lat},${long}.json`
return (dispatch) => {
fetch(url)
.then(res => res.json())
.then(data => dispatch(fetchConditionsSuccess(data)))
.catch(err => dispatch(fetchConditionsFailure(err)))
}}
reducer.js reducer.js
export default function (state={}, action){
switch(action.type){
case FETCH_CONDITIONS:
return {isLoading: true};
case FETCH_CONDITIONS_SUCCESS:
return {data:action.data}
case FETCH_CONDITIONS_FAILURE:
return{ error : true};
default :
return state
}}
component.js component.js
function mapStateToProps(state){
console.log("state" + state.conditionsReducer)
return{
weather : state.conditionsReducer
}}
export default connect(mapStateToProps, {getConditions})(Conditions)
app.js app.js
const store = createStore(rootReducer, applyMiddleware(thunk))
export default class App extends Component{
constructor(props){
super(props)
this.state = {isOpen : false}
this.getSideMenu = this.getSideMenu.bind(this)
}
getSideMenu(){
this.setState({isOpen : true})
}
render(){
return(
<Provider store={store}>
<SideMenu
menu={UpdateCity}
isOpen = {this.state.isOpen}
>
<StatusBar
hidden={true}
/>
<MainScreen isOpen={this.getSideMenu}/>
</SideMenu>
</Provider>
)
}
}
Thanks for your help 谢谢你的帮助
您正在控制台中获取[Object, Object]
,因为您正在将对象数组转换为String(执行String + var返回一个字符串),请尝试在控制台中正确显示:
console.log("state :", state.conditionsReducer)
You are getting [Object Object] because your reducer is storing next state in form {data:action.data}
which is an object. 之所以得到[Object Object],是因为减速器以
{data:action.data}
形式存储一个对象的下一个状态。 So try console.log(state.conditionsReducer.data)
or console.log("state :"state.conditionsReducer.data)
rather than console.log("state" + state.conditionsReducer)
. 因此,请尝试使用
console.log(state.conditionsReducer.data)
或console.log("state :"state.conditionsReducer.data)
而不是console.log("state" + state.conditionsReducer)
。
console.log
accepts any number of parameters, so just send each piece as its own param. console.log
可以接受任意数量的参数,因此只需将每个参数作为其自己的参数发送即可。
console.log("state", state.conditionsReducer.data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.