React-Router getUserConfirmation功能问题

时间:2018-02-14 09:25:11

标签: reactjs react-router react-redux material-ui react-router-dom

当用户单击浏览器后退按钮时,我正在尝试观察React Router状态更改。 我的应用程序集成了redux Provider和Material-UI。但是我无法让getUserConfirmation工作。

请在下面找到我的代码段。

 const getConfirmation = (message, callback) => {
      const allowTransition = window.confirm(message)
      strong textcallback(allowTransition)
    }
    const myHistory = createBrowserHistory();

    ReactDOM.render(
      <Provider store={store} >
        <MuiThemeProvider>
           <BrowserRouter basename="/app" history={myHistory} getUserConfirmation={getConfirmation} >
              <App />
           </BrowserRouter>
        </MuiThemeProvider>
      </Provider>, document.getElementById('root'));

二手图库版本:

React v16
react-router-dom v4.2.2
react-redux v5.0.6
material-ui v0.20.0
redux v3.7.2

我提到的一些文档:

https://reacttraining.com/react-router/web/api/BrowserRouter
https://codepen.io/pshrmn/pen/MpOpEY
https://codepen.io/anon/pen/gRXzMg?editors=0010

感谢您的帮助,谢谢

1 个答案:

答案 0 :(得分:1)

嗯..也许您也需要组件,这是我的演示  希望这是有用的。

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link, Prompt } from 'react-router-dom';
import { Row, Col, Modal } from 'antd';
import { Home, About, Topics } from './components';


const getConfirm = (content, callback) => {
    Modal.confirm({
        title: 'confirm',
        content,
        onOk: () => {
            callback(true);
        },
        onCancel: () => {
            callback(false);
        }
    });
};
class App extends Component {
    render() {
        return (
            <Router getUserConfirmation={getConfirm}>
                <div className="App">
                    <Prompt message="Are you sure you want to leave?" />
                    <Row>
                        <Col>
                            <Link to="/">Home</Link>
                        </Col>
                        <Col>
                            <Link to="/about">About</Link>
                        </Col>
                        <Col>
                            <Link to="/topics">Topics</Link>
                        </Col>
                        <Col>
                            <Link to="/with-state">withState</Link>
                        </Col>
                    </Row>
                    <Route path="/" exact component={Home} />
                    <Route path="about" component={About} />
                    <Route path="/topics" component={Topics} />
                </div>
            </Router>
        );
    }
}

export default App;