简体   繁体   English

数据未通过 redux-saga 加载到 UI

[英]Data not loading to the UI with redux-saga

With my react-native application when I call the API endpoint it is not loading the payload to the UI but I can see that the data is being loaded through the console.对于我的本机应用程序,当我调用 API 端点时,它不会将有效负载加载到 UI,但我可以看到数据正在通过控制台加载。 Can anybody please help me to sort the issue?有人可以帮我解决问题吗? This is killing me.这要死我了。 I am listing my sagas and reducers here.我在这里列出了我的传奇和减速器。

app.js应用程序.js

/**
 * @format
 * @flow
 */

import React, { Component } from 'react'
import { Provider } from 'react-redux'

import { store } from './store/store'

import Home from './screens/home/home'

class App extends Component {

  render() {
    return (
      <Provider store={store}>
        <Home />
      </Provider>
    )
  }
}

export default App

store.js商店.js

import { createStore, applyMiddleware } from 'redux'
import { createLogger } from 'redux-logger'
import createSagaMiddleware from 'redux-saga'

import rootReducer from '../reducers'

import rootSaga from '../sagas'

const sagaMiddleware = createSagaMiddleware()

const store = createStore(
  rootReducer,
  applyMiddleware(
    sagaMiddleware,
    createLogger(),
  ),
)

sagaMiddleware.run(rootSaga)

export {
  store,
}

actions.js动作.js

export function getTransatcions() {
    return { type: "TRANSACTIONS_REQUESTED" }
}

sagas.js sagas.js

import { takeEvery, call, put } from "redux-saga/effects"

export default function* watcherSaga() {
  yield takeEvery("TRANSACTIONS_REQUESTED", workerSaga)
}

function* workerSaga() {
  try {
    const payload = yield call(getTransatcions)
    yield put({ type: "TRANSACTIONS_LOADED", payload })
  } catch (e) {
    yield put({ type: "TRANSACTIONS_ERRORED", payload: e })
  }
}

function getTransatcions() {
    return fetch("https://jsonplaceholder.typicode.com/posts").then(response => {
        return response.json()
    })
}

reducers.js减速器.js

const initialState = {
    isFetching: false,
    transactions: []
}

export default (state = initialState, action) => {
    switch (action.type) {
        case "TRANSACTIONS_REQUESTED":
            return {
                ...state,
                isFetching: true
            }
        case "TRANSACTIONS_LOADED":
            return {
                ...state,
                isFetching: false,
                transactions: action.payload                
            }
        case "TRANSACTIONS_ERRORED":
            return {
                ...state,
                isFetching: false,
                error: action.error
            }
        default:
            return state
    }
}

home.js (UI Screen) home.js(用户界面屏幕)

import React, { Component } from 'react'
import { connect } from "react-redux"
import {
    View,
    Text,
    Button,
    StatusBar,    
    ScrollView,
    SafeAreaView    
} from 'react-native'

import { Header } from 'react-native/Libraries/NewAppScreen'

import { getTransatcions } from '../../actions'

import styles from './styles/home'

class Home extends Component {

    state = {
        transactions: []
    }

    componentWillMount() {
        this.props.getTransatcions()
    }

    render() {
        return (
            <>
                <StatusBar barStyle="dark-content" />
                <SafeAreaView>
                    <ScrollView
                        style={styles.scrollView}
                        contentInsetAdjustmentBehavior="automatic"                
                    >
                        <Header />
                        <View style={styles.body}>      
                            <Text>{this.props.transactions[0].title}</Text>      
                            {this.props.transactions && this.props.transactions.map(each => {
                                <>                                    
                                    <Text>{each.title}</Text>
                                </>
                            })}
                            <Button
                                title="Press me"
                                // onPress={actionCreators.getTransactions}
                            />
                        </View>
                    </ScrollView>
                </SafeAreaView>
            </>  
        )
    }   
}

function mapStateToProps(state) {
    return {
        transactions: state.transactions
    }
}

export default connect(mapStateToProps, { getTransatcions })(Home)

Can somebody please help me to fix this issue.有人可以帮我解决这个问题。 I had to use saga and i am bit new to redux-saga concepts.我不得不使用 saga,而且我对 redux-saga 概念有点陌生。 Data is being viewed in the UI If I put it like this.数据正在用户界面中查看 如果我这样说。

this.props.transactions[0].title

But If I use to repeat all content in the UI like this it won't show the data.但是如果我用这样重复 UI 中的所有内容,它就不会显示数据。

{this.props.transactions && this.props.transactions.map(each => {
<>                                    
    <Text>{each.title}</Text>
</>
})}

You missed return inside map你错过了map内的return

{this.props.transactions && this.props.transactions.map(each => {
   return (<>                                    
     <Text>{each.title}</Text>
   </>)
})}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM