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

Standalone React components #920

Closed
vvo opened this issue Mar 8, 2016 · 12 comments
Closed

Standalone React components #920

vvo opened this issue Mar 8, 2016 · 12 comments

Comments

@vvo
Copy link
Contributor

vvo commented Mar 8, 2016

--- NOTE FROM MAINTAINERS ---
(Sept 2016) We are working on react-instantsearch, it's already working. Ping me on twitter: https://twitter.com/vvoyer if you want to be an alpha tester
--- / NOTE FROM MAINTAINERS ---

instantsearch.js is currently a collection of smart JavaScript widgets rendering dumb React components.

  • smart as "I know how to manipulate the helper to refine this brand list"
  • dumb as "Give me a data list and a click function and I will display them"

This worked well for a lot of people but not for people already using React (or Angular, React Native, ractive, vuejs or any framework with his own concepts like state, scope, JSX ..).

To progressively solve this situation, we should first try to build the famous instant search demo with React only and see how it goes.

It would be a first POC at making instantsearch.js React friendly.

To avoid making instantsearch.js React friendly ONLY (and miss Angular or others), we should be careful about how we organize code. For instance, the function knowing how to refine a facet should be extracted and reused inside the component.

The ultimate goal of this POC is to be able to (not actual API, only for issue):

import React, { Component } from 'react' 
import { InstantSearch, SearchBox, Hits } from 'instantsearch.js'

class MySearchComponent extends Component {
  render() {
    return (
      <InstantSearch
        appId={ appId }
        apiKey={ apiKey }
        indexName={ indexName }
      >
        <SearchBox
          placeholder={ 'Custom placeholder' }
        />
        <Hits
          hitsPerPage={ 6 }
          renderHit={ hit => <MyItemComponent item={ hit } /> }
        />
      </InstantSearch>
    );
  }
}

While still being able to do:

// From react
import { InstantSearch, SearchBox, Hits, Pagination } from 'instantsearch.js/react';
// From another environment
import instantsearch from 'instantsearch.js';

Relevant discussion: #833

@vvo vvo added the POC needed label Mar 8, 2016
@redox
Copy link
Contributor

redox commented Mar 8, 2016

All in 👍

@mwdwrd
Copy link

mwdwrd commented Mar 30, 2016

+1

@jrasanen
Copy link

jrasanen commented Apr 6, 2016

👍

I bumped into this issue too today.

Thinking of either writing my own components in React using the JS api only and not using instantsearch.js. Or use this & kind of awkwardly append widget templates as strings inside a component.

Probably gonna do the latter and keep watching this thread.

@eyeinsky
Copy link

Not to insist, but what's the current estimate on getting this? :)

@vvo
Copy link
Contributor Author

vvo commented Apr 26, 2016

Hi @eyeinsky no news nor estimate for the moment we are trying hard to make this happen of course we want to allow our React users to easily start with Algolia.

If you have a specific React + Algolia question in the meantime feel free to drop it here.

@darrylmack
Copy link

In the absence of standalone React Components, what is the current best practice for incorporating Algolia search(instant or not) with React applications?

@darrylmack
Copy link

darrylmack commented Jun 15, 2016

HI @vvo

In React using ComponentDidMount works fine to retrieve the sample Best Buy data. -

import React from 'react'
import {render} from 'react-dom'
import algoliasearch from 'algoliasearch'

var client = algoliasearch('(app id)', '(api key)')
var index = client.initIndex('instant_search')

const App = React.createClass({
componentDidMount: function() {
index.search('iphone', function searchDone(err,content) {
console.log(err, content);
})
},
render() {
return (





Search

      <h2>Best result:</h2>
      <div id="best-result"></div>
  </div>
)

}
})

render(<App/>, document.getElementById('root'))

But using an event handler doesn't - Same Code

import React from 'react'
import {render} from 'react-dom'
import algoliasearch from 'algoliasearch'

var client = algoliasearch('(app id)', '(api key)')
var index = client.initIndex('instant_search')

const App = React.createClass({
handleSubmit: function() {
index.search('iphone', function searchDone(err,content) {
console.log(err, content);
})
},
render() {
return (





<button className="btn btn-primary"onClick={this.handleSubmit} >Search

      <h2>Best result:</h2>
      <div id="best-result"></div>
  </div>
)

}
})

render( <App/>, document.getElementById('root'))

Any idea why this is the case?

@vvo
Copy link
Contributor Author

vvo commented Jun 15, 2016

Hi @darrylmack, thanks a lot for reaching out here concerning your issue with react.

Can you open a new issue describing your problem only? It will ease the process

@ram535ii
Copy link

I agree, this would be really useful. As @jrasanen said, I'll also "kind of awkwardly append widget templates as strings inside a component", but since I'm using Angular ideally I'd like it to play nice with that.

What I would have wanted to do is this:

 instantsearch.widgets.hits({
      container: '#hits-container',
      templates: {
        empty: 'No results',
         item: "<ci-search-hit-dtv object-id=‘{{objectID}}'> </ci-search-hit-dtv>"
      },
      hitsPerPage: 6
    })

I was actually suprised when it didn't work, but clearly I don't understand HoganJS. Is this kind of thing what you are alluding to in this issue?

@darrylmack
Copy link

Hi @vvo

I got it working. I can capture a field value and submit and I get a json with the expected search results. I think the macro issue is that in React I have to wire up the resulting components. So a SearchResultsContainer -> SearchResult -> [ResultTitle, ResultDescription, ResultImage, etc. ].

Not hard to do but you guys clearly get React(see instantsearch.js). It would be great if there were prebuilt components for Algolia, i.e. SidebarComponent for faceted search, SearchResultsComponent that contained a child component with an individual SearchResult and so on.

The second issue is attaching instantsearch to a div id. Ideally we can just attach it to a component without specifying an id or ref.

Thanks though for making this great product. It will greatly enhance our application.

@vvo
Copy link
Contributor Author

vvo commented Jun 17, 2016

Hi @ram535ii and @darrylmack thanks for all your feedback. Just to be clear, today we do not have any pre-built React components and using instantsearch.js in React in not recommended.

Reusable React components will be made available via the creation of instantsearch.js/React in July.

Thanks for reaching out, I am locking this issue, if you want to discuss more please open a new issue.

@algolia algolia locked and limited conversation to collaborators Jun 17, 2016
@vvo
Copy link
Contributor Author

vvo commented Sep 14, 2016

(Sept 2016) We are working on react-instantsearch, it's already working. Ping me on twitter: https://twitter.com/vvoyer if you want to be an alpha tester

@vvo vvo closed this as completed Sep 14, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants