Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React: update TypeScript support article #5570

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,39 @@ DevExtreme React components are supplied with TypeScript declarations. Strict ty
The following code shows an example of using TypeScript with DevExtreme components:

<!-- tab: App.tsx -->
import React from 'react';
import React, { useState } from 'react';
import List from 'devextreme-react/list';

import 'devextreme/dist/css/dx.light.css';

interface IListItemProps {
text: string;
}

interface IComponentState {
counter: number;
}

const items: IListItemProps[] = [
{ text: "Item 1" },
{ text: "Item 2" },
{ text: "Item 3" }
];

class Item extends React.Component<IListItemProps, IComponentState> {
constructor(props: IListItemProps) {
super(props);
this.state = {
counter: 0
};
this.addCounter = this.addCounter.bind(this);
}

public render() {
return (
<div onClick={this.addCounter}>
{this.props.data.text} was clicked {this.state.counter} times
</div>
);
}

private addCounter() {
this.setState({
counter: this.state.counter + 1
});
}
}

class App extends React.Component {
render() {
return (
<List items={items} itemComponent={Item} />
);
}
}
const Item: React.FC<IListItemProps> = (props) => {
const [counter, setCounter] = useState<number>(0);

const addCounter = () => {
setCounter(counter + 1);
};

return (
<div onClick={addCounter}>
{props.text} was clicked {counter} times
</div>
);
};

const App: React.FC = () => {
return (
<List items={items} itemRender={(props: any) => <Item {...props.data} />} />
);
};

export default App;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
To import component-specific types, use the `ComponentTypes` declaration where `Component` is the component name:
To get component-specific types, import the `ComponentTypes` declaration where `Component` is the component name:

<!-- tab: App.tsx -->
import DateBox, { DateBoxTypes } from "devextreme-react/date-box";
import DateBox, { DateBoxTypes } from 'devextreme-react/date-box';

const dateType: DateBoxTypes.DateType = "datetime";
const dateType: DateBoxTypes.DateType = 'datetime';

function App() {
return (
Expand All @@ -12,4 +12,20 @@ To import component-specific types, use the `ComponentTypes` declaration where `
}
</script>

You can also [import component-specific types](/concepts/Common/Modularity/02%20DevExtreme%20Modules%20Structure '/Documentation/Guide/Common/Modularity/DevExtreme_Modules_Structure/') from the `devextreme/ui` package.
If you need the same type for multiple components, you can also import this type from `common` submodule:

<!-- tab: App.tsx -->
// In the sample below, ValidationRule is imported for each component:

import { DataGridTypes } from 'devextreme-react/data-grid';
import { FormTypes } from 'devextreme-react/form';

const dataGridValidationRule: DataGridTypes.ValidationRule;
const formValidationRule: FormTypes.ValidationRule;

// In the sample below, ValidationRule is imported from the common submodule:

import { ValidationRule } from 'devextreme-react/common';

const dataGridValidationRule: ValidationRule;
const formValidationRule: ValidationRule;