My code is:
/* @flow */
export default class MyClass {
constructor(myParam: string) {
this.myParam = myParam
}
}
Error being produced at https://flow.org/try/ is following:
5: this.myParam = myParam
^ property `myParam`. Property not found in
5: this.myParam = myParam
^ MyClass
@AlokBansal8 Flow forces you to specify types of your properties in class block like
/* @flow */
export default class MyClass {
myParam: string;
constructor(myParam: string) {
this.myParam = myParam
}
}
@TrySound Any thoughts on doing this if you're dynamically creating properties in your constructor...? I'm looping through an array to create a bunch of refs like so: (trying some fancy-pants animations where I need exact CSS properties and coordinates, so that's why I'm using so many refs)
import projects from '../data/projects'
...
constructor(props: Props) {
super(props)
Object.keys(projects).forEach((projectName) => {
this[`${projectName}Ref`] = React.createRef()
})
}
So when you do myParam: string
there in the class block, do you know of a way to do that as dynamically in a loop somehow...? Thanks.
[key: string]: React.Ref<any>
Thanks, but that's giving me a parsing error. Maybe I'm using it wrong?
class WorkPane extends React.Component<Props> {
[key: string]: React.Ref<any>
constructor(props: Props) {
super(props)
Object.keys(projects).forEach((projectName) => {
this[`${projectName}Ref`] = React.createRef()
})
}
Looks like my use case may be related to https://github.com/facebook/flow/issues/252
Exact same use case as you @elramus but getting this annoying error! :(
Most helpful comment
@AlokBansal8 Flow forces you to specify types of your properties in class block like