首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Native Algolia即时搜索和SearchBox

React Native Algolia即时搜索和SearchBox
EN

Stack Overflow用户
提问于 2019-09-06 06:01:10
回答 1查看 1.7K关注 0票数 2

在遵循Algolia文档(https://www.algolia.com/doc/api-reference/widgets/react/)使用以下代码设置一个简单的Alogila索引搜索时

代码语言:javascript
复制
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {Text, View, StyleSheet, Button, TextInput} from 'react-native';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, Index, SearchBox, Hits } from 'react-instantsearch-dom';


const searchClient = algoliasearch(
    '**********', //app ID
    '************************' //app key
);

class MyApp extends Component {
    constructor(props){
        super(props);

    }

    render(){
        return (
            <View style={{
                flex: 1,
                alignItems: "center",
                flexDirection: "column",
                paddingTop: 20
              }}>
            <InstantSearch
                searchClient={searchClient}
                indexName="dev_INVENTORY">
                    <SearchBox/>
            </InstantSearch>
            </View>
    )}

};

const styles = StyleSheet.create({});

export default MyApp;

我收到错误‘不变冲突:没有找到名称输入的视图配置。请确保组件名称以大写字母开头。

此错误位于输入中(由Searchbox创建)....‘

当我删除SearchBox代码时,应用程序运行正常,但我一添加它就遇到了错误,但显然所有元素都正确地大写了(除非我做了一个可笑的愚蠢的疏忽!?)我想知道这个错误是否与这个人的问题有关;Using Algolia react-instantsearch with react-native,但我不这么认为。

如果任何人有任何建议,只是为了让我可以开始搜索Algolia,那将是王牌,因为我有点卡住了!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-12 17:42:55

问题是你正在尝试使用react-instantsearch-dom的一些部件,这些部件与React Native不兼容。

对于React Native,您需要使用连接器而不是小部件:https://www.algolia.com/doc/guides/building-search-ui/going-further/native/react/#using-connectors-instead-of-widgets

在您的示例中,它应该提供类似以下内容:

代码语言:javascript
复制
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View, StyleSheet, TextInput } from 'react-native';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, connectSearchBox } from 'react-instantsearch-native';

const searchClient = algoliasearch(
  '**********', // app ID
  '************************' // app key
);

class SearchBox extends Component {
  render() {
    return (
      <View>
        <TextInput
          onChangeText={text => this.props.refine(text)}
          value={this.props.currentRefinement}
          placeholder={'Search a product...'}
          clearButtonMode={'always'}
          spellCheck={false}
          autoCorrect={false}
          autoCapitalize={'none'}
        />
      </View>
    );
  }
}

SearchBox.propTypes = {
  refine: PropTypes.func.isRequired,
  currentRefinement: PropTypes.string,
};

const ConnectedSearchBox = connectSearchBox(SearchBox);

class MyApp extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <View
        style={{
          flex: 1,
          alignItems: 'center',
          flexDirection: 'column',
          paddingTop: 20,
        }}
      >
        <InstantSearch searchClient={searchClient} indexName="dev_INVENTORY">
          <ConnectedSearchBox />
        </InstantSearch>
      </View>
    );
  }
}

const styles = StyleSheet.create({});

export default MyApp;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57813463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档