Realizing Dependency Inversion Principle with Flow in React.

Tomoharu Tsutsumi
1 min readJan 3, 2021

--

We can make our codes static with Flow.

Of course, by making codes static, we can use “Interface” like Java. The example is below. This is a child component.

// @flowtype Props = {
name: string,
age: number
}
export default class Person extends React.Component<Props> {
constructor(props: Props) {
super(props);
this.state = {
name: props.person.name,
age: props.person.age,
};
}
render() { return(
<div>
{this.state.name}
{this.state.age}
</div>
)
}
}

And the next one is a parent component.

import Person from './Person.js'export default class Person extends React.Component<Props> {
constructor(props: Props) {
super(props);
this.state = {
name: name // getting by API,
age: age // getting by API,
};
}
componentDidMount(){
// Some APIs
}
render() { return(
<div>
<Person name={this.state.name} age={this.state.age}
</div>
)
}
}

Both components depend on the interface(type Props). It is not necessary for the components to depend on each other with Flow.

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)