Answer the question
In order to leave comments, you need to log in
Should you use hooks like useState when you're already managing state with Redux?
I ask you not to throw stones strongly, I'm just learning React, and so far one idea is not clear.
I create a pet project:
const Router = (props) => {
return (
<div>
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/new" component={NewToDo} />>
<Route component={NotFound} />
</Switch>
</BrowserRouter>
</div>
);
};
useDispatch()
and useSelector()
, which control the global state. Question : does the useState hook make sense if you are already using the redux store? const [formatData, setFormatData] = useState({});
useEffect(() => {
const dateKeys = ["weekday", "day", "month", "year"];
(function () {
const ruDate = new Intl.DateTimeFormat("ru", { ------ получаю дату
day: "2-digit",
month: "long",
year: "numeric",
weekday: "long",
})
.format(new Date())
.replace(/,/, "") ----//убираю и меняю символы
.replace(/в/, "В")
.replace(/с/, "С")
.replace(/\sг./, "")
.split(" "); -------- создаю массив
const result = Object.assign( ----- привожу массив к виду объект: {ключ: свойство}
...dateKeys.map((k, i) => ({ [k]: ruDate[i] }))
);
setFormatData(result);
})();
}, []);
.replace(/,/, "") ----//убираю и меняю символы
.replace(/в/, "В")
.replace(/с/, "С")
.replace(/\sг./, "")
Answer the question
In order to leave comments, you need to log in
In a nutshell, redux is for the global state of the entire application (for example, storing table data), and hooks for the state of individual components (such as tabs). But this is not a panacea. Personally, I have an application that only uses state hooks.
Don't use props to push dispatch and store from redux. For this, as you correctly noted, there is useSelector and useDispatch.
1. useState is very necessary despite the use of redux. For example, your site displays a notification when you click on a button. Why push notification display status in redux? Redux, as rightly noted, is like a global store. There is no need for this, for the display status (true / false) it is enough to use useState inside the component.
2. The question was not quite correctly formulated.
If your function is used only in a specific component, store it in it.
If a function is used in several components of the same level, move the function to a higher level where these components are imported.
If a function is often used in different places, put it in a separate file (utils) and import it into the necessary components.
Question: Is this sausage a shit code?
1. Redux / any other state manager for global states and storing a large amount of data
2. useState - for the state of the component, for example, switching the flag by which the active class is added for the element
I use this approach when there is a Redux / MobX / Recoli application /etc. + Dan Abramov himself spoke about this approach. (HolyJS 2020)
Everyone has already said about redux, often there is a need to use useState in the presence of redux and this is normal.
About replace . Because you can pass a function to replace for a string as a second parameter, then here you can clearly use this:
make an object where the key would be a substring from the match, and the value would be what it needs to be replaced with and then, by coincidence, substitute the desired value from the object, approximately So:
let matches = {
",": "",
"в": "В",
"с": "С",
" г.": ""
}
.replace(/с|в|\sг./g, (match) => matches[match])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question