I am implementing an example from https://github.com/moroshko/react-autosuggest
Important code is as follows:
import React, { Component } from 'react'; import suburbs from 'json!../suburbs.json'; function getSuggestions(input, callback) { const suggestions = suburbs .filter(suburbObj => suburbMatchRegex.test(suburbObj.suburb)) .sort((suburbObj1, suburbObj2) => suburbObj1.suburb.toLowerCase().indexOf(lowercasedInput) - suburbObj2.suburb.toLowerCase().indexOf(lowercasedInput) ) .slice(0, 7) .map(suburbObj => suburbObj.suburb);
This copy-paste code from the example (which works) has an error in my project:
Error: Cannot resolve module 'json' in /home/juanda/redux-pruebas/components
If I choose the json prefix !:
import suburbs from '../suburbs.json';
Thus, during compilation, I did not receive errors (import completed). However, when I executed it, I got errors:
Uncaught TypeError: _jsonfilesSuburbsJson2.default.filter is not a function
If I debug it, I see that the suburbs are an object c, not an array, so the filter function is not defined.
However, in the above example, sentences are an array. If I rewrite such sentences, everything works:
const suggestions = suburbs var suggestions = [ { 'suburb': 'Abbeyard', 'postcode': '3737' }, { 'suburb': 'Abbotsford', 'postcode': '3067' }, { 'suburb': 'Aberfeldie', 'postcode': '3040' } ].filter(suburbObj => suburbMatchRegex.test(suburbObj.suburb))
So ... what a json! does the prefix do in the import?
Why can't I put it in my code? Some Babel configuration?