Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router history#push and #replace should accept optional hash second parameter #309

Open
catmando opened this issue May 18, 2020 · 0 comments
Open

Comments

@catmando
Copy link
Contributor

@catmando catmando commented May 18, 2020

should be able to do history.push('/my-page', foo: :manchu) and have the url be /my-page?foo=manchu same with replace

The following patch to Hyperstack::Internal::Router can be added to your hyper_component.rb file:

module Hyperstack
  module Router
    class History
      def add_query_params(path, query_params)
        return path if query_params.empty?

        params = query_params.collect do |k, v|
          "#{`encodeURIComponent(#{k})`}=#{`encodeURIComponent(#{v})`}"
        end.join('&')
        "#{path}#{path =~ /\?/ ? '&' : '?'}#{params}"
      end

      def push(path, query_params = {})
        `#{@native}.push(#{add_query_params(path, query_params)})`
      end

      def replace(path, query_params = {})
        `#{@native}.replace(#{add_query_params(path, query_params)})`
      end
    end
  end
end

module React
  class Router
    class History
      def create_browser_history
        Hyperstack::Router::History.new(`#{@native}.createBrowserHistory()`)
      end

      def create_hash_history
        Hyperstack::Router::History.new(`#{@native}.createHashHistory()`)
      end

      def create_memory_history
        Hyperstack::Router::History.new(`#{@native}.createMemoryHistory()`)
      end
    end
  end
end

Note: The second patch to React::Router::HIstory makes the push/replace work in the top level router history class method as well.


What was happening was create_memory_history was an alias_native of createMemoryHistory. When alias_native is used, the result if not an object that maps directly to ruby will be automatically wrapped in a NativeObject instance. This is also what Hyperstack::Router::History does, but it also adds other methods to the instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.