【发布时间】:2018-05-18 21:05:57
【问题描述】:
我构建了一个 react 组件,它将 Json 文件导入到数组中以映射结果。我需要在另一个组件中使用该数组。我不知道我是否必须在新组件中构建这个组件,或者是否有一种方法可以导出所需的数组(数据)。数组源每 4 秒更新一次。
感谢您的帮助。
我的第一个组件是:
import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class Ramas extends React.Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
componentDidMount() {
const fetchData = () => {
axios
.get('http://localhost:8888/dp_8/fuente/procesos_arbol.json')
.then(({ data })=> {
this.setState({
data: data
});
console.log(data);
})
.catch(()=> {console.log('no recibido');});
};
fetchData();
this.update = setInterval(fetchData, 4000);
} // final componentDidMount
render() {
const initialData = this.state.data.map((el) => {
return (
<p>id={ el.id } | name - { el.name } | padre - {el.parent}</p>
);
});
return (<div className="datos_iniciales">
{ initialData }
</div>);
}
}
ReactDOM.render(
<Ramas />,
document.getElementById('container')
);
【问题讨论】:
-
这对你有用吗?您是在问是否有更好的解决方案,或者您是否遇到任何错误?
-
不,罗迪乌斯。我想在另一个组件中像数据源一样重用该数组。
标签: reactjs export components