Reactjs 如何将Redux与React一起使用

Reactjs 如何将Redux与React一起使用,reactjs,redux,Reactjs,Redux,我只想从api中获取数据并在前端显示它。我使用Redux调用api,使用它的操作和减缩器。在reducer中,我将initalstate作为空数组。当成功调用API时,我将更新存储状态。下面是有助于轻松理解概念的实用程序 store.js import { createStore } from 'redux'; import reducer from './reducers/reducer'; let store = createStore(reducer) export default st

我只想从api中获取数据并在前端显示它。我使用Redux调用api,使用它的操作和减缩器。在reducer中,我将initalstate作为空数组。当成功调用API时,我将更新存储状态。下面是有助于轻松理解概念的实用程序

store.js

import { createStore } from 'redux';
import reducer from './reducers/reducer';

let store = createStore(reducer)
export default store
import { 
 FETCH_IMAGES_SUCCESS
} from './actionTypes'

export function fetchImages() {
  return dispatch => {
   return fetch("https://api.com/data")
   .then(res => res.json())
   .then(json => {
     dispatch(fetchImagesSuccess(json.posts));
    return json.posts;
   })
  };
}

export const fetchImagesSuccess = images => ({
  type: FETCH_IMAGES_SUCCESS,
  payload: { images }
});
import {
 FETCH_IMAGES_SUCCESS
} from '../actions/actionTypes'

const initialState = {
 images:[] 
}

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case FETCH_IMAGES_SUCCESS:
     return {...state,images:action.payload.images}
    default:
     return state
  }
}

export default reducer;
actions.js

import { createStore } from 'redux';
import reducer from './reducers/reducer';

let store = createStore(reducer)
export default store
import { 
 FETCH_IMAGES_SUCCESS
} from './actionTypes'

export function fetchImages() {
  return dispatch => {
   return fetch("https://api.com/data")
   .then(res => res.json())
   .then(json => {
     dispatch(fetchImagesSuccess(json.posts));
    return json.posts;
   })
  };
}

export const fetchImagesSuccess = images => ({
  type: FETCH_IMAGES_SUCCESS,
  payload: { images }
});
import {
 FETCH_IMAGES_SUCCESS
} from '../actions/actionTypes'

const initialState = {
 images:[] 
}

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case FETCH_IMAGES_SUCCESS:
     return {...state,images:action.payload.images}
    default:
     return state
  }
}

export default reducer;
reducer.js

import { createStore } from 'redux';
import reducer from './reducers/reducer';

let store = createStore(reducer)
export default store
import { 
 FETCH_IMAGES_SUCCESS
} from './actionTypes'

export function fetchImages() {
  return dispatch => {
   return fetch("https://api.com/data")
   .then(res => res.json())
   .then(json => {
     dispatch(fetchImagesSuccess(json.posts));
    return json.posts;
   })
  };
}

export const fetchImagesSuccess = images => ({
  type: FETCH_IMAGES_SUCCESS,
  payload: { images }
});
import {
 FETCH_IMAGES_SUCCESS
} from '../actions/actionTypes'

const initialState = {
 images:[] 
}

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case FETCH_IMAGES_SUCCESS:
     return {...state,images:action.payload.images}
    default:
     return state
  }
}

export default reducer;
现在,请告诉我,我需要做什么才能称之为Redux操作和 从API获取数据。我正在使用React显示数据。


谢谢。

您需要一个类似或的中间件来与使用Redux维护的操作和全局存储进行对话。 您可以按照本教程进行操作:

如果您要使用Redux Thunk,则需要修改存储分配,如下所示:

const store = createStore(rootReducer, applyMiddleware(thunk));
现在,为所有父组件创建一个容器

import { connect  } from 'react-redux';
import App from '../components/App';


export function mapStateToProps(appState) {

  return {
  /* this is where you get your store data through the reducer returned 
  state */
  };
}

export function mapDispatchToProps(dispatch) {
  return {
   // make all your action dispatches here
// for ex: getData(payload) => dispatch({type: GETDATA, payload: payload})

  };
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

您需要像or这样的中间件来与使用Redux维护的操作和全局存储进行对话。 您可以按照本教程进行操作:

如果您要使用Redux Thunk,则需要修改存储分配,如下所示:

const store = createStore(rootReducer, applyMiddleware(thunk));
现在,为所有父组件创建一个容器

import { connect  } from 'react-redux';
import App from '../components/App';


export function mapStateToProps(appState) {

  return {
  /* this is where you get your store data through the reducer returned 
  state */
  };
}

export function mapDispatchToProps(dispatch) {
  return {
   // make all your action dispatches here
// for ex: getData(payload) => dispatch({type: GETDATA, payload: payload})

  };
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

在使用页面中,您可以使用MapStateTops等函数并连接以实现此目的

在使用页面中,您可以使用MapStateTops等函数并连接以实现此目的

正如Mustafa所说,您需要使用
MapStateTops
。让我解释一下

您刚才所做的只是主存储的配置(redux中只有一个)。现在您需要在组件中使用它,但是如何使用呢?创建组件时,
存储
的内容将在
容器
的帮助下作为
道具
传递

容器是将商店与react组件链接起来的方式

也就是说,您需要安装
redux
react redux
。在上面的代码中,您已经成功地使用
redux
library配置了还原器。现在需要
react-redux
来创建容器(包装react组件)

下面是一个如何将这一切结合在一起的示例:

正如Mustafa所说,您需要使用
MapStateTops
。让我解释一下

您刚才所做的只是主存储的配置(redux中只有一个)。现在您需要在组件中使用它,但是如何使用呢?创建组件时,
存储
的内容将在
容器
的帮助下作为
道具
传递

容器是将商店与react组件链接起来的方式

也就是说,您需要安装
redux
react redux
。在上面的代码中,您已经成功地使用
redux
library配置了还原器。现在需要
react-redux
来创建容器(包装react组件)

下面是一个如何将这一切结合在一起的示例:

您需要使用与下面代码类似的MapStateTrops。假设您的减速器称为测试,它是状态的一部分

const mapStateToProps = (state, props) =>
({
    router: props.router,
    test: state.test
});

然后,
test
将用作React类中的属性。显然,您需要为React包含相应的导入。

您需要使用类似于下面代码的mapStateToProps。假设您的减速器称为测试,它是状态的一部分

const mapStateToProps = (state, props) =>
({
    router: props.router,
    test: state.test
});


然后,
test
将用作React类中的属性。显然,您需要为React包含相应的导入。

我不需要文档链接。你能用简短的语言解释我吗?@Rajat,这只是一个玩笑lazy@Rajat坐下来,首先阅读关于Redux和react Redux(react和Redux之间的连接器)的内容。文档实际上就是为了这个。因此,这意味着当您在编程上无法完成文档之外的工作时,可能不需要文档链接。你能用简短的语言解释我吗?@Rajat,这只是一个玩笑lazy@Rajat坐下来,首先阅读关于Redux和react Redux(react和Redux之间的连接器)的内容。文档实际上就是为了这个。因此,它适用于当您在使用redux thunk时无法以编程方式完成文档之外的工作的情况。请解释我如何从React前端调用Redux操作,然后获取返回的状态?我在todo示例中看到“”。什么是Provider?我需要在react中使用它吗?@Rajat Provider将存储注入根级别的组件。这就是作文。我想你需要仔细检查一下文件。这里有很好的解释,你能编辑你上面的代码吗,你在哪里使用了redux thunk?因为您没有导入它并告诉如何在store中使用它。谢谢。@Rajat检查我答案中的第一行代码。中间件是在创建存储时分配的。这是在index.jsi上完成的,我正在使用redux thunk。请解释我如何从React前端调用Redux操作,然后获取返回的状态?我在todo示例中看到“”。什么是Provider?我需要在react中使用它吗?@Rajat Provider将存储注入根级别的组件。这就是作文。我想你需要仔细检查一下文件。这里有很好的解释,你能编辑你上面的代码吗,你在哪里使用了redux thunk?因为您没有导入它并告诉如何在store中使用它。谢谢。@Rajat检查我答案中的第一行代码。中间件是在创建存储时分配的。这是在index.js上完成的。谢谢你的回答。但是你能告诉我怎么做吗?你能和我分享一些参考资料吗。但是请不要分享文档链接,因为它们很长。我会提供一个例子,给我一分钟。当然,请这样做。完成:如果你觉得它像它一样有用^^谢谢你的回答。但是你能告诉我怎么做吗?你能和我分享一些参考资料吗。但请不要共享文档链接,因为它们很长。我会提供一个例子,给我一分钟。当然,请