score:1
For React class components, you will generally need to supply the generic prop parameters with the types for the props and state respectively.
class App extends Component<AppProps, AppState> {
...
}
For your case, since there are no props for the App
component, you will only need to do this:
class App extends Component<{}, AppState> {
constructor(props: {}) {
super(props);
this.state: AppState = {
input: "",
imageUrl: "",
};
}
...
}
score:1
if you want to try it with the hooks then you can do this.
import React, { useState } from "react";
interface AppState {
input: string;
imageUrl: string;
}
const App: React.FC<AppState> = () => {
const [input, setInput] = useState("");
const [imageUrl, setImageUrl] = useState("");
const handleOnButtonSubmit = () => {
setImageUrl(input);
clarifaiApp.models
.predict(
Clarifai.COLOR_MODEL,
// URL
"https://samples.clarifai.com/metro-north.jpg"
)
.then(
function(response: any) {
console.log("This is your response: ", response);
},
function(err: Error) {
console.log("There was an error: ", err);
}
);
};
return (
<Container>
<ImageLinkForm
onInputChange={(e: any) => setInput(e.target.value)}
onButtonSubmit={handleOnButtonSubmit}
/>
<FaceRecognition imageUrl={imageUrl} />
</Container>
);
};
export default App;
score:2
If you want to initialize the state inside the constructor
of a class component you have to provide two type argument to the React.Component
generic. The first argument is meant to provide type information about the props and the second argument is meant to provide the type information about the state
. For example,
interface AppProps {
// props
}
type AppState = {
input: string;
imageUrl: string;
}
class App extends Component<AppProps,AppState> {
// you don't need any more explicit type annotation here
constructor(props) {
super(props);
this.state = {
input: "",
imageUrl: "",
};
}
// rest of the app logic
}
This would not have caused any issue if you would have initialized the state in the state field outside the constructor.
type AppState = {
input: string;
imageUrl: string;
}
class App extends Component {
state: AppState = {
input: "",
imageUrl: ""
}
// rest of the app logic
}
In the second case, you can choose to provide a type argument for the props if the component is expecting some props. For example,
interface AppProps {
// type info for props
}
type AppState = {
input: string;
imageUrl: string;
}
class App extends Component<AppProps> {
state: AppState = {
input: "",
imageUrl: ""
}
// rest of the app logic
}
Source: stackoverflow.com
Related Query
- TypeScript errors in React Class Component property does not exist on type 'Readonly<{}>', not sure how to set types for state
- react hooks and typescript - Property '***' does not exist on type 'never'
- TS2339: Property 'focus' does not exist on type '{}'. with React typescript
- Property 'name' does not exist on type 'EventTarget' - React + TypeScript
- Typescript Property '' does not exist on type 'never'. in React NodeJs stack
- Property 'location' does not exist on type 'Readonly<{}> - React Router and Typescript
- React Typescript property does not exist on type / IntrinsicAttributes & IntrinsicClassAttributes
- React Typescript Custom Hooks: Property 'prop_name' does not exist on type '{}'
- TypeScript Property 'value' does not exist on type 'HTMLElement'. React Jest Testing
- Reusable layout component in TypeScript React: Property 'path' does not exist on type 'IntrinsicAttributes & IProps'
- TypeScript error - property component does not exist on React mui MenuItem
- How to remove Property 'length' does not exist on type '{}' in React Typescript
- Property 'elements' does not exist on type 'EventTarget' in React Typescript
- React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes
- React TypeScript Property 'assign' does not exist on type 'Object constructor'
- Property 'props' does not exist on type '{}' (trying to get access to props of children) - React Typescript
- React typescript : Property 'path' does not exist on type 'IntrinsicAttributes & Pick<any, never>'
- Unable to set ref due to error "TS2339: Property 'X' does not exist on type 'Y' " inside a react component
- React Typescript property does not exist on type during array map
- Reactjs finding `interface` issue with react component class as `Property 'show' does not exist on type 'Readonly<{}>`
- React with TypeScript - Property 'value' does not exist on type 'EventTarget'
- React typescript Property 'map' does not exist on type 'User'
- Error rewriting as a React Functional Component in Typescript: Property 'forceUpdateHandler' does not exist on type 'MutableRefObject<Spinner | null>'
- Property "handle" does not exist on type "undefined" - react context and typescript
- React Router typescript Property 'redirectUrl' does not exist on type '{}'
- Typescript React Redux - property does not exist on Action type
- Typescript provides error on react key property - Property 'key' does not exist on type
- React Native & TypeScript : Property 'focus' does not exist on type 'RefObject<TextInput>'
- Property does not exist on type react typescript
- Typescript react redux, property does not exist on type {}
More Query from same tag
- TypeError: Cannot read property 'map' of undefined brings down in the entire system
- TypeError: props.theTodos.map is not a function
- How to toggle (change multiple times) data by using useState and onClick in React?
- React: break dependency between 2 related contexts with top-level constant object
- Does React state get saved before setting it with useState?
- How to disable keyboard appearing on mobile : React date time picker
- Module parse failed, unexpected token with Webpack
- Create a pure function to merge objects
- How to use backgroundImage css in styled component having typescript
- How to make caret bigger in React, bootstrap?
- When to use private/protected methods in Typescript with React
- Dynamic table values from array of objects in react 17.0.01
- How to use javaScript file as Higher Order wrapper for other JavaScript File
- Is there any way to control the useEffect result with API request which is frequently invoked?
- staggerChildren with framer motion
- React and TypeScript - Component<Props & {}>
- How do I place two JSX elements next to each other?
- Module not found: Can't resolve './pages' Failed to compile
- Material-UI: Prevent Accordion summary vertical movement
- Redux Form: Getting Access to Field Components meta.touched property inside JSX Form
- $.post(url, data) doesn't work
- How can I disable back button in React Router?
- useEffect to rerender this component
- Set Input value in quotes if there is two or more words
- How i can to convert array to object of array in react?
- how to get to the key in an array of objects?
- just select one checkbox in reactjs
- How do I access styles object inside React container?
- React does not display time only {"${new Date().getHours()}:${new Date().getMinutes()}"}
- How to get the origin of current URL using react-router-dom?