如何在反应中使用API返回的js文件中的函数?

How to use the function from the js file returned by an API in react?

我一直在使用 create-react-app

这是 componentDidMount() 中的示例请求:

axios.get("/")
  .then(res => {
    const test = res.data;
  })
  .catch(err => console.log(err));

我想使用 API 的响应,这是一个 javascript 代码。

这是 API 的示例 return:

const sample = props => (React.createElement('div'))

我想要实现的是使用 sample 函数并将数据传递给它并使其显示在当前组件中

render() {
  return test.sample(SomeData)
}

这样做似乎很不对,但您可以在响应中使用 eval:

eval(res.data);

Here 是一篇关于为什么你不应该使用 eval.

的文章