2016-08-02 11 views
12

मैं एक्सप्रेस और वेबपैक के साथ, ReactJS + Redux का उपयोग कर रहा हूं। एक एपीआई बनाया गया है, और मैं क्लाइंट-साइड से - आरईएसटी कॉल करने में सक्षम होना चाहता हूं - पोस्ट, पोस्ट, पुट, डिलीट करें।ReactJS + Redux एप्लिकेशन से रीस्ट कॉल कैसे ठीक से करें?

रेडक्स आर्किटेक्चर के साथ ऐसा करने का सही तरीका क्या है और क्या है? प्रवाह के किसी भी अच्छे उदाहरण, reducers, कार्रवाई निर्माता, स्टोर, और प्रतिक्रिया मार्गों के मामले में, बेहद सहायक होगा।

अग्रिम धन्यवाद!

उत्तर

10

सबसे आसान तरीका है, इसे redux-thunk पैकेज का उपयोग करके करना है। इस पैकेज एक redux मिडलवेयर है, इसलिए सब से पहले, आप इसे redux से कनेक्ट करना चाहिए:

import { createStore, applyMiddleware } from 'redux'; 
import thunk from 'redux-thunk'; 
import rootReducer from './reducers/index'; 

const store = createStore(
    rootReducer, 
    applyMiddleware(thunk) 
); 

यह आपको नियमित sync कार्यों के साथ-साथ async कार्यों प्रेषण करने के लिए अनुमति देता है। के लिए उनमें से एक बनाएँ:

// actions.js 

export function fetchTodos() { 
    // Instead of plain objects, we are returning function. 
    return function(dispatch) { 
    // Dispatching REQUEST action, which tells our app, that we are started requesting todos. 
    dispatch({ 
     type: 'FETCH_TODOS_REQUEST' 
    }); 
    return fetch('/api/todos') 
     // Here, we are getting json body(in our case it will contain `todos` or `error` prop, depending on request was failed or not) from server response 
     // And providing `response` and `body` variables to the next chain. 
     .then(response => response.json().then(body => ({ response, body }))) 
     .then(({ response, body }) => { 
     if (!response.ok) { 
      // If request was failed, dispatching FAILURE action. 
      dispatch({ 
      type: 'FETCH_TODOS_FAILURE', 
      error: body.error 
      }); 
     } else { 
      // When everything is ok, dispatching SUCCESS action. 
      dispatch({ 
      type: 'FETCH_TODOS_SUCCESS', 
      todos: body.todos 
      }); 
     } 
     }); 
    } 
} 

मैं अलग करने के लिए पसंद करते हैं प्रतिक्रिया प्रस्तुतिकरण और कंटेनर घटकों पर घटकों। इस दृष्टिकोण को पूरी तरह से this article में वर्णित किया गया था।

अगला, हमें TodosContainer घटक बनाना चाहिए, जो प्रस्तुतिकरण Todos घटक को डेटा प्रदान करेगा। यहाँ, हम react-redux लाइब्रेरी का उपयोग कर रहे हैं:

// TodosContainer.js 

import React, { Component } from 'react'; 
import { connect } from 'react-redux'; 
import { fetchTodos } from '../actions'; 

class TodosContainer extends Component { 
    componentDidMount() { 
    // When container was mounted, we need to start fetching todos. 
    this.props.fetchTodos(); 
    } 

    render() { 
    // In some simple cases, it is not necessary to create separate `Todos` component. You can put todos markup directly here. 
    return <Todos items={this.props.todos} /> 
    } 
} 

// This function is used to convert redux global state to desired props. 
function mapStateToProps(state) { 
    // `state` variable contains whole redux state. 
    return { 
    // I assume, you have `todos` state variable. 
    // Todos will be available in container component as `this.props.todos` 
    todos: state.todos 
    }; 
} 

// This function is used to provide callbacks to container component. 
function mapDispatchToProps(dispatch) { 
    return { 
    // This function will be available in component as `this.props.fetchTodos` 
    fetchTodos: function() { 
     dispatch(fetchTodos()); 
    } 
    }; 
} 

// We are using `connect` function to wrap our component with special component, which will provide to container all needed data. 
export default connect(mapStateToProps, mapDispatchToProps)(TodosContainer); 

इसके अलावा, आप todosReducer बनाना चाहिए, जो FETCH_TODOS_SUCCESS कार्रवाई संभाल लेंगे, और अन्य 2 कार्यों यदि आप प्रदर्शन लोडर/त्रुटि संदेश चाहते हैं।

// reducers.js 

import { combineReducers } from 'redux'; 

const INITIAL_STATE = { 
    items: [], 
    isFetching: false, 
    error: undefined 
}; 

function todosReducer(state = INITIAL_STATE, action) { 
    switch (action.type) { 
    case 'FETCH_TODOS_REQUEST': 
     // This time, you may want to display loader in the UI. 
     return Object.assign({}, state, { 
     isFetching: true 
     }); 
    case 'FETCH_TODOS_SUCCESS': 
     // Adding derived todos to state 
     return Object.assign({}, state, { 
     isFetching: false, 
     todos: action.todos 
     }); 
    case 'FETCH_TODOS_FAILURE': 
     // Providing error message to state, to be able display it in UI. 
     return Object.assign({}, state, { 
     isFetching: false, 
     error: action.error 
     }); 
    default: 
     return state; 
    } 
} 

export default combineReducers({ 
    todos: todosReducer 
}); 
अन्य कार्यों के लिए CREATE तरह

, UPDATE, DELETE कुछ खास नहीं है, वे उसी तरह कार्यान्वित कर रहे हैं नहीं है।

+0

सहायता के लिए आपको बहुत बहुत धन्यवाद। अभी भी अवधारणा को समझने की कोशिश कर रहा है। आप घटक से कार्रवाई कैसे और कहां कहते हैं? क्या आप '.then (प्रतिक्रिया => response.json()। (शरीर => ({प्रतिक्रिया, शरीर}) पर कुछ और स्पष्टीकरण दे सकते हैं)) .थ (({प्रतिक्रिया, शरीर}) => { 'कर रहा है? धन्यवाद फिर से –

+0

@ जोको, हाँ, मैं जल्द ही उत्तर अपडेट कर दूंगा। – 1ven

+0

@ जोको, अपडेट किया गया जवाब – 1ven

0

यह redux-thunk, redux-saga, और redux-observable जैसे पुस्तकालयों के लिए प्राथमिक उपयोग केस है।

import fetch from 'isomorphic-fetch' 

export const REQUEST_POSTS = 'REQUEST_POSTS' 
function requestPosts(subreddit) { 
    return { 
    type: REQUEST_POSTS, 
    subreddit 
    } 
} 

export const RECEIVE_POSTS = 'RECEIVE_POSTS' 
function receivePosts(subreddit, json) { 
    return { 
    type: RECEIVE_POSTS, 
    subreddit, 
    posts: json.data.children.map(child => child.data), 
    receivedAt: Date.now() 
    } 
} 

// Meet our first thunk action creator! 
// Though its insides are different, you would use it just like any other action creator: 
// store.dispatch(fetchPosts('reactjs')) 

export function fetchPosts(subreddit) { 

    // Thunk middleware knows how to handle functions. 
    // It passes the dispatch method as an argument to the function, 
    // thus making it able to dispatch actions itself. 

    return function (dispatch) { 

    // First dispatch: the app state is updated to inform 
    // that the API call is starting. 

    dispatch(requestPosts(subreddit)) 

    // The function called by the thunk middleware can return a value, 
    // that is passed on as the return value of the dispatch method. 

    // In this case, we return a promise to wait for. 
    // This is not required by thunk middleware, but it is convenient for us. 

    return fetch(`http://www.reddit.com/r/${subreddit}.json`) 
     .then(response => response.json()) 
     .then(json => 

     // We can dispatch many times! 
     // Here, we update the app state with the results of the API call. 

     dispatch(receivePosts(subreddit, json)) 
    ) 

     // In a real world app, you also want to 
     // catch any error in the network call. 
    } 
} 

ऊपर के उदाहरण http://redux.js.org/docs/advanced/AsyncActions.html से सीधे लिया जाता है जो वास्तव में अपने प्रश्न पर जवाब के लिए निश्चित स्रोत है: जहां कुछ इस तरह करना होगा

redux-thunk, सरलतम है।

+0

के साथ रीडक्स-क्रूड-स्टोर के स्रोत को भी पढ़ सकते हैं, तो थंक वास्तव में इसके बारे में इतना खास क्या करता है? ऐसा लगता है कि आप बस पुस्तकालयों के बिना एपीआई के यूआरएल में लाने के लिए बस() कर सकते हैं। –

+0

'रेडक्स-थंक' वास्तुशिल्प रूप से रेडक्स में एसिंक्रोनस व्यवहार को एकीकृत करने के लिए उपयोगी है, जो सिंक्रोनस है। नेटवर्क लाने के लिए 'fetch' पर्याप्त है, लेकिन यह आसान हिस्सा है। जब आप रेडक्स एप्लिकेशन से कॉल कैसे करें, इस बारे में पूछना शुरू करते हैं, तो आपको उस रेडक्स आर्किटेक्चर में उस व्यवहार को शामिल करने के लिए 'redux-thunk' जैसी कुछ चाहिए। –

+0

वास्तव में स्पष्टीकरण की सराहना करते हैं! अन्य शब्दों में लाने का अधिकार सही है? फिर पोस्ट, पुट, और डिलीट क्या होगा? –

1

संक्षिप्त उत्तर है:

  1. redux एक वास्तुकला
  2. आप किसी भी पुस्तकालय का उपयोग कर सकते नहीं है। इन दिनों बहुत से लोग सीधे fetch API का उपयोग करते हैं।
  3. एसिंक्रोनस क्रियाओं के साथ रेडक्स को एकीकृत करने में सक्षम होने के लिए (जिसे आपको AJAX की आवश्यकता है), आपको मदद करने के लिए लाइब्रेरी का उपयोग करने की आवश्यकता है। सबसे लोकप्रिय दो redux-thunk और redux-saga हैं, जैसा कि अन्य ने कहा है।

एक मस्तिष्क-मृत सरल पुस्तकालय के लिए जिसे आप अपने रेडक्स ऐप में छोड़ सकते हैं, आप redux-crud-store को आजमा सकते हैं। अस्वीकरण: मैंने इसे लिखा था।यदि आप रेडक्स-सागा

संबंधित मुद्दे