为什么 Thunk-Redux 将对象转换为字符串?
Why is Thunk-Redux turning an object into a string?
我在使用 thunk-redux 时遇到了一个奇怪的问题。我正在编写一个 React-Redux 应用程序,它调用 public API,并在 table 中显示数据。但是,当我合并 thunk 中间件 来处理异步 API 调用时,我的数据在操作被分派到 reducer 后被字符串化。
index.js(动作创作者)
export const FETCHING_DATA = 'FETCHING_DATA';
export const FETCH_SUCCESS = 'FETCH_SUCCESS';
export const ERROR = 'ERROR';
export const getData = () => {
return {
type : FETCHING_DATA
}
}
export const getDataSuccess = (data) => {
return {
type : FETCH_SUCCESS,
payload: data
}
}
export const getDataFailure = () => {
return {
type : ERROR
}
}
export function searchCVE(cve){
const url = `${CVE_URL}api/cve/${cve}`;
return dispatch => {
dispatch(getData());
fetch(PROXY + url)
.then(blob => blob.json())
.then(data => {
console.log('Request: ', data);
dispatch(getDataSuccess(data))
})
.catch(e => {
console.log(e);
dispatch(getDataFailure(e.message))
});
}
}
data_reducer.js(减速机)
import {FETCHING_DATA ,FETCH_SUCCESS, ERROR } from '../actions/index.js';
const initialState = {
payload:[],
fetching: false,
error: false
}
export default function(state=initialState, action){
console.log('Got CVE: ', action);
switch (action.type){
case FETCHING_DATA: return {payload:[], fetching: true, ...state}
case FETCH_SUCCESS: return [action.payload, ...state]
case ERROR: return {payload:[], error: true, ...state}
}
return state;
}
正如您在index.js动作创建器中看到的,console.log('Request: ', data);
显示了我想要的JSON对象。但是,当我在我的 table 组件中 {console.log('TEST: ' + this.props.cve)}
时,控制台显示:
TEST: [object Object]
在我的应用程序中,我从来没有 "stringifying" 我的数据 - why/where thunk-redux 可以将我的数据转换为字符串吗?我感谢社区提供的任何见解。
At no point in my app am I "stringifying" my data - why/where could thunk-redux be turning my data into a string?
redux-thunk
在任何情况下都不能这样做。这是deadly simple;它所做的只是以不同的方式处理函数操作。
问题是对象正在被你字符串化,+
加法运算符将对象强制转换为字符串:
{console.log('TEST: ' + this.props.cve)}
如果希望在控制台中显示一个对象,它应该是:
{console.log('TEST: ', this.props.cve)}
或者可以显示在DOM:
<pre>{JSON.stringify(this.props.cve, null, 2)}</pre>
我在使用 thunk-redux 时遇到了一个奇怪的问题。我正在编写一个 React-Redux 应用程序,它调用 public API,并在 table 中显示数据。但是,当我合并 thunk 中间件 来处理异步 API 调用时,我的数据在操作被分派到 reducer 后被字符串化。
index.js(动作创作者)
export const FETCHING_DATA = 'FETCHING_DATA';
export const FETCH_SUCCESS = 'FETCH_SUCCESS';
export const ERROR = 'ERROR';
export const getData = () => {
return {
type : FETCHING_DATA
}
}
export const getDataSuccess = (data) => {
return {
type : FETCH_SUCCESS,
payload: data
}
}
export const getDataFailure = () => {
return {
type : ERROR
}
}
export function searchCVE(cve){
const url = `${CVE_URL}api/cve/${cve}`;
return dispatch => {
dispatch(getData());
fetch(PROXY + url)
.then(blob => blob.json())
.then(data => {
console.log('Request: ', data);
dispatch(getDataSuccess(data))
})
.catch(e => {
console.log(e);
dispatch(getDataFailure(e.message))
});
}
}
data_reducer.js(减速机)
import {FETCHING_DATA ,FETCH_SUCCESS, ERROR } from '../actions/index.js';
const initialState = {
payload:[],
fetching: false,
error: false
}
export default function(state=initialState, action){
console.log('Got CVE: ', action);
switch (action.type){
case FETCHING_DATA: return {payload:[], fetching: true, ...state}
case FETCH_SUCCESS: return [action.payload, ...state]
case ERROR: return {payload:[], error: true, ...state}
}
return state;
}
正如您在index.js动作创建器中看到的,console.log('Request: ', data);
显示了我想要的JSON对象。但是,当我在我的 table 组件中 {console.log('TEST: ' + this.props.cve)}
时,控制台显示:
TEST: [object Object]
在我的应用程序中,我从来没有 "stringifying" 我的数据 - why/where thunk-redux 可以将我的数据转换为字符串吗?我感谢社区提供的任何见解。
At no point in my app am I "stringifying" my data - why/where could thunk-redux be turning my data into a string?
redux-thunk
在任何情况下都不能这样做。这是deadly simple;它所做的只是以不同的方式处理函数操作。
问题是对象正在被你字符串化,+
加法运算符将对象强制转换为字符串:
{console.log('TEST: ' + this.props.cve)}
如果希望在控制台中显示一个对象,它应该是:
{console.log('TEST: ', this.props.cve)}
或者可以显示在DOM:
<pre>{JSON.stringify(this.props.cve, null, 2)}</pre>