Giter VIP home page Giter VIP logo

Comments (13)

alisd23 avatar alisd23 commented on July 26, 2024 5

DONE

πŸŽ‰ 🎈 πŸ… 🍺 πŸŽ‰ 🎈 πŸ… 🍺 πŸŽ‰ 🎈 πŸ… 🍺 πŸŽ‰ 🎈 πŸ… 🍺 πŸŽ‰ 🎈 πŸ… 🍺 πŸŽ‰ 🎈 πŸ… 🍺 πŸŽ‰ 🎈 πŸ… 🍺

from mobx-react-router.

chuan137 avatar chuan137 commented on July 26, 2024 2

I am using a very simple hack to achieve routing.

import React, { Component } from 'react';
import { Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import { observable, computed } from 'mobx';


class RouterStore {
  constructor(props) {
    this.history = createHistory();
  }

  push(to) {
    this.history.push(to);
  }
}


class SyncedRouter extends Component {
  render() {
    return (
      <Router history={this.props.history} children={this.props.children} />
    )
  }
}

export { SyncedRouter };
export default new RouterStore();

then provide store to the mounted Dom by

ReactDOM.render(
  <Provider routing={routerStore}>
    <SyncedRouter history={routerStore.history}>
      <App />
    </SyncedRouter>
  </Provider>,
  document.getElementById('root')
);

later in the code, I can access the router history through injected routing property,

this.props.routing.push('/login')

from mobx-react-router.

alisd23 avatar alisd23 commented on July 26, 2024 2

@chuan137 Hey thanks, this looks good. I hadn't had a chance to get round to checking the new rr4 packages situation - I didn't realise react-router-dom was it's own package now.

Either way this is probably the right approach, some sync router component as you've done, then we can pretty much keep the API exactly the same.

Shouldn't be too much work, I'll try get something up and running in the next few days πŸ‘

from mobx-react-router.

alisd23 avatar alisd23 commented on July 26, 2024 2

PR is up for v4 bindings - #14
πŸŽ‰ 🎈

from mobx-react-router.

varHarrie avatar varHarrie commented on July 26, 2024 1

@chuan137 I got error with your solution:
There is no reference of pathname in my code.

TypeError: Cannot read property 'pathname' of undefined

new Router
http://localhost:8080/index.js:56484:61
http://localhost:8080/index.js:40012:18
measureLifeCyclePerf
http://localhost:8080/index.js:39792:12
ReactCompositeComponentWrapper._constructComponentWithoutOwner
http://localhost:8080/index.js:40011:16
ReactCompositeComponentWrapper._constructComponent
http://localhost:8080/index.js:39997:21
ReactCompositeComponentWrapper.mountComponent
http://localhost:8080/index.js:39905:21
Object.mountComponent
http://localhost:8080/index.js:47061:35
ReactCompositeComponentWrapper.performInitialMount
http://localhost:8080/index.js:40088:34
ReactCompositeComponentWrapper.mountComponent
http://localhost:8080/index.js:39975:21
Object.mountComponent
http://localhost:8080/index.js:47061:35
import * as React from 'react'
import {Router} from 'react-router-dom'
import {History} from 'history'

export default class SyncedRouter extends React.Component<{
  history: History
}, void> {
  render () {
    return (
      <Router history={this.props.history} children={this.props.children}/>
    )
  }
}
import {createHistory, History} from 'history'
import {observable} from 'mobx'

class RouterStore {
  @observable history: History = null

  constructor () {
    this.history = createHistory()
  }
}

const routerStore = new RouterStore()

export default routerStore
const stores = {
  router: routerStore
}

export default class Root extends React.Component<void, void> {
  render () {
    return (
      <Provider store={stores}>
        <SyncedRouter history={stores.router.history}>
          <Switch>
            <Route path='/' component={App}/>
            <Route component={Page404}/>
          </Switch>
        </SyncedRouter>
      </Provider>
    )
  }
}

from mobx-react-router.

alisd23 avatar alisd23 commented on July 26, 2024 1

@abelkbil Make sure to follow the setup guide correctly, including correct package versions (react-router, mobx, etc...). If that doesn't work I think it is worth asking this on Stack Overflow. GitHub is supposed to just be for issue tracking and feature requests.

from mobx-react-router.

alisd23 avatar alisd23 commented on July 26, 2024

In short - hopefully soon. RR4 is still in beta, so the API may still change somewhat. Once it is properly released I'll be happy to create a v4 of this package, confident that I won't have to change it again πŸ‘.

I'll leave this issue open though so it can be tracked by others.

from mobx-react-router.

petejkim avatar petejkim commented on July 26, 2024

v4 is out

from mobx-react-router.

alisd23 avatar alisd23 commented on July 26, 2024

I'll be looking to get a v4 version out at some point soon. No guarentees on what that will be though. Fairly busy at the moment.

Welcoming any suggestions/discussion here on how it should look. The React Router v4 API is very different from v3.

from mobx-react-router.

mattblackdev avatar mattblackdev commented on July 26, 2024

@chuan137 Nice work. Still trying to wrap my head around react-router. Could you share what's going on inside this guy:

import createHistory from 'history/createBrowserHistory';

from mobx-react-router.

chuan137 avatar chuan137 commented on July 26, 2024

@mattblackapps I havn't dive into it too much either. Simply speaking, History is a standalone library, which can be used to track the browsing location. It has intuitive navigation api, like push, go, goBack and etc. It is developed by the same developers as react-router, and also used there. Head to their page https://github.com/ReactTraining/history if you want more detail.

@alisd23 I feel the same way, that it should not be too much work. Version 4 is quite elegant organised.

from mobx-react-router.

abelkbil avatar abelkbil commented on July 26, 2024

@varHarrie , I am also in the same situation, could you please clear how to fix this.
Thanks in advance

from mobx-react-router.

abelkbil avatar abelkbil commented on July 26, 2024

I used alternative way:

import * as PropTypes from 'prop-types';

class SingleDashboardHeaderNav extends React.Component<Props, State> {

  static contextTypes = {
      router: PropTypes.object
  };

editDashboard() {
    this.context.router.history.push(`/dashboard/edit/${this.dashboardStore.currentDashboard.id}`);
  }

  createNewDashboard() {
    this.dashboardStore.createNewDashboard('')
    .then(response => {
      if ( response.status ) {
        this.context.router.history.push(`/dashboard/edit/${response.dashboard.id}`);
      }
    });
  }

I dont know is it the way to use.
May be useful for some one..

from mobx-react-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.