Answer the question
In order to leave comments, you need to log in
How to use states inside a UI component?
In general, I decided to make a small UI library for myself, for example, on Tailwind.
I have it in a separate NPM package or connected via npm link.
There are a couple of situations when a component should have its own local state, the question is how to implement this?
I have functional components like:
import React, { useState } from "react";
export const TestBlock = () => {
const [state, setState] = useState(null);
return (
<button onClick={() => setState(Date.now())}>
blablabla
</button>
)
}
import React from "react";
export class TestBlockClass extends React.Component<{}, { timestamp: number }> {
constructor(props: any) {
super(props);
this.state = {
timestamp: 0
};
}
render() {
return (
<>
<div>
Time {this.state.timestamp}
</div>
<button onClick={() => this.setState({timestamp: Date.now()})}>
тык
</button>
</>
)
}
}
Answer the question
In order to leave comments, you need to log in
in general, the problem is in npm link
to solve it, you need to add in the webpack config:
resolve: {
extensions: [".ts", ".tsx", ".js"],
alias: {
react: path.resolve('./node_modules/react'),
},
},
https://codesandbox.io/s/qna-q-1107640-miil1 is not reproduced, so something was not said.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question