gpt4 book ai didi

javascript - 能够在控制台中查看 JSON,但不知道如何在 View 中显示 - React-Redux

转载 作者:行者123 更新时间:2023-11-28 05:00:42 25 4
gpt4 key购买 nike

我仍在学习 React-Redux。我了解如何检索简单的 JSON 数组。但是,我不确定如何调用嵌套对象。我正在尝试获取标题并在控制台中查看此内容:

  • 对象
    • 数据:对象
      • 数据:对象
        • 数据:对象
          • 之后:“t3_5t0hy2”之前:nullchildren:数组[25]
            • 0:对象
              • 数据:对象
                • 标题:“我在构建的每个网站上使用的 Google Analytics 设置(作者:Philip Walton)”

dataAction.js

import axios from 'axios';


export function fetchData(){
return function(dispatch){
axios.get("https://www.reddit.com/r/webdev/top/.json")
.then((response) => {
dispatch({ type: "FETCH_DATA_FULFILLED", payload: response.data})
})
.catch((err) => {
dispatch({type: "FETCH_DATA_REJECTED", payload: err})
})
}
}

export function addData(id, text){
return {
type: 'ADD_DATA',
payload:{
id,
title,
},
}
}

export function updateData(id, text){
return {
type: 'UPDATE_DATA',
payload: {
id,
title,
},
}
}

export function deleteData(id){
return {
type: 'DELETE_DATA',
payload: id
}
}

Layout.js(组件)

import React from "react"
import { connect } from "react-redux"

import { fetchUser } from "../actions/userActions"
import { fetchPartner } from "../actions/projectActions"
import { fetchData } from "../actions/dataActions"

@connect((store) => {
return {
user: store.user.user,
userFetched: store.user.fetched,
partner: store.partner.partner,
partnerFetched: store.partner.fetched,
data: store.data.data
};
})
export default class Layout extends React.Component {
componentWillMount() {
this.props.dispatch(fetchUser())
this.props.dispatch(fetchPartner())
this.props.dispatch(fetchData())
}


render() {
const { user, partner, data } = this.props;



//const mappedData = data.map(data => <li>{data.title}</li>)

return <div>
<h1>{user.name}{user.age}</h1>
<h1>{partner.title}</h1>
<ul>{data.title}</ul>
</div>
}
}

Reducer.js

export default function reducer(state={
data: {
data: {}
},
fetching: false,
fetched: false,
error: null,
}, action) {

switch(action.type){
case "FETCH_DATA":{
return {...state, fetching:true}
}
case "FETCH_DATA_REJECTED":{
return {...state, fetching: false, error: action.payload}
}
case "FETCH_DATA_FULFILLED":{
return {...state, fetching: false, fetched: true, data: action.payload}
}
case "ADD_DATA":{
return {...state, data: [...state.data, action.payload]}
}
case "UPDATE_DATA":{
const { id, title } = action.payload
const newData = [...state.data]
const dataToUpdate = newData.findIndex(data => data.id === id)
newData[dataToUpdate] = action.payload;

return {...state, data: newData}
}
case "DELETE_DATA":{
return {...state, data: state.data.filter(data => data.id !== action.payload)}
}
}
return state
}

当这个问题解决后,下一步将是迭代该对象,我也不确定如何实现。

最佳答案

当您发送有效负载:response.data时,您可以进一步了解对象结构并在有效负载中发送实际数据。

一旦你发送了有效负载,你将需要一个reducer来改变状态。请按照本教程了解如何创建 reducer 。 http://blog.scottlogic.com/2016/05/19/redux-reducer-arrays.html

一旦状态更新,您将让代码从状态读取值。一旦状态发生变化,React 将自动更新或渲染,您可以编写逻辑。

关于javascript - 能够在控制台中查看 JSON,但不知道如何在 View 中显示 - React-Redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42151375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com