I am asking this question from a best practice point of view - the answers I've found on here are about more specific problems to an individual code base. I am happy to be pointed in the right direction if it's already been answered or to be shown another way if what I am attempting is not considered good practice - I may have misunderstood some concepts...
I am learning React and am building a simple app using it. Mainly in order to keep my code tidy, I have created two files. First file - I access an API from inside my component and render some data from my components state. Second file - I want to use the same info from the components state in my first file and use that as props in my second file.
A simplified example is below.
First file:
import React, { Component } from 'react';
import SecondComponent from './SecondComponent';
function DisplayFirstData (props) {
return (
{props.data}
}
class FirstComponent extends Component {
constructor(props) {
super(props);
this.state = {
myData: something,
}
}
render() {
<DisplayFirstData data={this.state.myData} />
}
}
Second component:
import React, { Component } from 'react';
function DisplaySecondData (props) {
return (
{props.data}
}
class SecondComponent extends Component {
constructor(props) {
super(props);
this.state = {
// State object from the first component file
}
}
render() {
<DisplaySecondData data={this.state.myData} />
}
}