javascript - 警告 : Memory Router ignores the history props

标签 javascript android react-native react-router react-router-redux

我正在研究 React Native 以构建应用程序。出于这个原因,我使用了 react-router-redux 但我收到了两个疯狂的警告,它说: 警告:忽略 history Prop 。要使用自定义历史记录,请使用“import {Router}”而不是“import{MemoryRouter as Router}”

这是我的代码:

我的 index.android.js:

import {AppRegistry} from 'react-native';
import Home from './app/components/Home';
AppRegistry.registerComponent('myCode', () => Home);

我的家:

import React, { Component } from 'react';
import { NativeRouter as Router, Route } from 'react-router-native';
import createHistory from 'history/createMemoryHistory';
import { Provider } from 'react-redux';
import { syncHistoryWithStore } from 'react-router-redux';

import configureStore from '../config/store';
import launch from './launch';
import Dinner from '../components/Dinner';
export default class Home extends Component {
    render() {
        const history = createHistory();
        const store = configureStore(history);
        const appHistory = syncHistoryWithStore(history, store);
        return (
            <Provider store={store}>
                <Router history={appHistory}>
                    <Route path="/" component={launch}>
                        <Route path="dinner" component={Dinner} />
                    </Route>
                </Router>
            </Provider>
        );
    }
}

这是我的 store.js:

import { applyMiddleware, createStore } from 'redux';
import { routerMiddleware  } from 'react-router-redux';
import thunk from 'redux-thunk';
import promiseMiddleware from 'redux-promise-middleware';

import reducer from '../reducers/index';

export default function configureStore(history) {
    const middleware = applyMiddleware(
        promiseMiddleware(),
        thunk,
        routerMiddleware(history));
    return createStore(reducer, {}, middleware);
}

我创建了一个支持所有类型 reducer 的 reducer :

const initialState = {
    data: {},
    error: null,
    fetching: false,
    fetched: false,
};

export default function reducer(state=initialState, action) {
    switch (action.type) {
        case (action.type.match(/^.*_PENDING$/) || {}).input:
            // Action is pending (request is in progress)
            return {...state, fetching: true};
        case (action.type.match(/^.*_FULFILLED$/) || {}).input:
            // Action is fulfilled (request is successful/promise resolved)
            return {
                ...state,
                data: action.payload.data,
                fetched: true,
                fetching: false};
        case (action.type.match(/^.*_REJECTED$/) || {}).input:
            // Action is rejected (request failed/promise rejected)
            return {
                ...state,
                error: action.payload,
                fetched: false,
                fetching: false
            };
        default:
            return state;
    }
};

最佳答案

通过删除 react-router-redux 包并安装 react-router-redux": "^5.0.0-alpha.6 问题解决了,但我在这部分关心的另一件事是这个版本没有syncHistoryWithStore 方法所以在这部分我改变了 App 是这样的:

import React, {Component} from 'react';
import {Route} from 'react-router-native';
import createHistory from 'history/createMemoryHistory';
import {Provider} from 'react-redux';
import {Switch} from 'react-router'
import {ConnectedRouter} from 'react-router-redux';

import configureStore from './store';
import Home from './components/Home';
import About from './components/About';

export default class App extends Component {
    render() {
        const history = createHistory();
        const store = configureStore(history);
        return (
            <Provider store={store}>
                <ConnectedRouter history={history}>
                    <Switch>
                        <Route path="/" component={Home}/>
                        <Route path="/about" component={About}/>
                    </Switch>
                </ConnectedRouter>
            </Provider>
        );
    }
}

关于javascript - 警告 : Memory Router ignores the history props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273964/

相关文章:

java - android studio 中 facebook api 的 doInBackground() 错误

java - 如何在 DialogFragment 中获取(或模拟)ViewPager?

javascript - react native Firebase图像上传错误404

react-native - 如何在运行时 react native 检测低性能设备

javascript - React Native - navigation.navigate 不是一个函数

javascript - Console.log 正在打印未请求的字符串

javascript - 使用 Webpack 4 在 ReactJs 项目中设置公共(public)路径

javascript - 如何对以下内容进行 @hapi/joi 验证?

javascript - xdan/datetimepicker - datetimepicker( {'setOptions' , {highlightedDates : [] } }) is not updated - calendar view is not updated

java - 使用 MY_SQL 时 Android Java 到 PHP 字符串文字问题