0

Say i have a file like below on the server eg Github. I want to download the file as string with fetch and then be able to convert it to an object that i could use.

OBS: all import and the interface exist on locally so there wont be any thing missing.

import parser from '../interface/parsers';
import '../interface/extension';

export default class test implements parser {
  public name: string;
  constructor() {
  
    }
  
  }
2
  • I am asking this question so that I can help you better. Why do you want to do this? Commented Jan 31, 2021 at 6:41
  • I have an app that can parse data from their other sites. each time I want to add/remove a parser I have to publish my app. I want to skip this and simple download the parser to the app dynamically. Do you have idees? Commented Jan 31, 2021 at 6:49

1 Answer 1

1

Here's an example that at least works in a Snack (ignored the fetch part and just used a local string):

export default function App() {
  
  const [evaledComponent, setEvaledComponent] = useState(null)

  useEffect(() => {
    this.React = React;
    this.Text = Text;  
    setEvaledComponent(eval('this.React.createElement(this.Text, {}, "test component")'));
  },[])

  return <Text>{evaledComponent}</Text>  

}

Note: I took a trick from from Why eval() is not working in React Native? assigning React to this.React in order to get it usable -- it's not a pretty hack and I'd recommend a more stable solution if you went ahead with this plan.

But:

This is an extremely uphill battle to take on and using eval is a terrible security risk since it executes arbitrary code. If there's another route you can take (like rendering things based on a JSON description), I would take that route instead.

Update:

Example of returning a custom object (non-React)

eval("var MyParser = { getString: () => {return 'hi there'} }");

return <Text>{MyParser.getString()}</Text>  
Sign up to request clarification or add additional context in comments.

3 Comments

please could you provide an example to my example, skip the fetch part. I just want to know since my class wont be under the folder parser how could I resolve the import in the class ?
You've included almost no code, so it's pretty difficult to say exactly what you're trying to do. But, I've updated my answer to show that you can load arbitrary code of any sort with eval (again, very unsafe). I'd also wager that doing extends with the result of your arbitrary code loading my be a challenge.
Yes i know, there is more import i have to inc, so i may make it auto update instead. I just wanted to know if its possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.