use-reducer

React Hooks useReduce or useState

纵然是瞬间 提交于 2021-02-08 09:23:20
问题 I have a form with 30 fields and all those fields are a template from a value in a dropdown, if I change the value of the template I will end up creating a custom template. So the action is: When we are changing a value from the form, the template will change in custom template As you can see there are some logic that come up and down and I'm worried about the multiple setState declaration and call. Should I use useReducer or useState ? 回答1: From the useReducer documentation: useReducer is

How to dispatch in useEffect?

老子叫甜甜 提交于 2021-01-29 11:32:54
问题 I tried to set a state using useState inside useEffect with no problem, code as follows: import React, {useState, useEffect} from 'react'; const Banner = () => { const [title, setTitle] = useState([]); useEffect(() => { async function fetchData() { const api = 'some api url'; let response = await fetch(api); response = await response.json(); setTitle(response.title); } fetchData(); }) } export default Banner; Now I'd like to do the same thing but with useReducer, I tried to modify the above

Why is useReducer's dispatch causing re-renders?

夙愿已清 提交于 2020-12-13 12:12:00
问题 Suppose I implement a simple global loading state like this: // hooks/useLoading.js import React, { createContext, useContext, useReducer } from 'react'; const Context = createContext(); const { Provider } = Context; const initialState = { isLoading: false, }; function reducer(state, action) { switch (action.type) { case 'SET_LOADING_ON': { return { ...state, isLoading: true, }; } case 'SET_LOADING_OFF': { return { ...state, isLoading: false, }; } } } export const actionCreators = {

Why is useReducer's dispatch causing re-renders?

旧城冷巷雨未停 提交于 2020-12-13 12:10:35
问题 Suppose I implement a simple global loading state like this: // hooks/useLoading.js import React, { createContext, useContext, useReducer } from 'react'; const Context = createContext(); const { Provider } = Context; const initialState = { isLoading: false, }; function reducer(state, action) { switch (action.type) { case 'SET_LOADING_ON': { return { ...state, isLoading: true, }; } case 'SET_LOADING_OFF': { return { ...state, isLoading: false, }; } } } export const actionCreators = {

Why is useReducer's dispatch causing re-renders?

孤街浪徒 提交于 2020-12-13 12:09:27
问题 Suppose I implement a simple global loading state like this: // hooks/useLoading.js import React, { createContext, useContext, useReducer } from 'react'; const Context = createContext(); const { Provider } = Context; const initialState = { isLoading: false, }; function reducer(state, action) { switch (action.type) { case 'SET_LOADING_ON': { return { ...state, isLoading: true, }; } case 'SET_LOADING_OFF': { return { ...state, isLoading: false, }; } } } export const actionCreators = {

React Navigation v5 Authentication Flows (Screens as Different Files)

女生的网名这么多〃 提交于 2020-06-29 03:48:55
问题 If we see in the doc example: https://reactnavigation.org/docs/auth-flow/ : function SignInScreen() { const [username, setUsername] = React.useState(''); const [password, setPassword] = React.useState(''); const { signIn } = React.useContext(AuthContext); // ???? return ( <View> <TextInput placeholder="Username" value={username} onChangeText={setUsername} /> <TextInput placeholder="Password" value={password} onChangeText={setPassword} secureTextEntry /> <Button title="Sign in" onPress={() =>

How to dynamically use useReducer when looping over a list?

[亡魂溺海] 提交于 2020-05-17 03:01:15
问题 I am trying to show a list of Times (e.g. 07:00, 07:30) but when a repeated time appears, show the number of repetitons by its side (e.g. 07:30, 08:00³) As I am looping over a list, each item will need its own state so that the counter can be set and displayed next to each Time At the moment, I am having trouble with too many rerenders, but I am also not sure if my reducer is correct The code without any comments can be seen in this repo: https://github.com/charles7771/decrease-number-wont

How to dynamically use useReducer when looping over a list?

∥☆過路亽.° 提交于 2020-05-17 03:00:31
问题 I am trying to show a list of Times (e.g. 07:00, 07:30) but when a repeated time appears, show the number of repetitons by its side (e.g. 07:30, 08:00³) As I am looping over a list, each item will need its own state so that the counter can be set and displayed next to each Time At the moment, I am having trouble with too many rerenders, but I am also not sure if my reducer is correct The code without any comments can be seen in this repo: https://github.com/charles7771/decrease-number-wont

How to dynamically use useReducer when looping over a list?

☆樱花仙子☆ 提交于 2020-05-17 02:59:37
问题 I am trying to show a list of Times (e.g. 07:00, 07:30) but when a repeated time appears, show the number of repetitons by its side (e.g. 07:30, 08:00³) As I am looping over a list, each item will need its own state so that the counter can be set and displayed next to each Time At the moment, I am having trouble with too many rerenders, but I am also not sure if my reducer is correct The code without any comments can be seen in this repo: https://github.com/charles7771/decrease-number-wont

React Hook useReducer always running twice

≡放荡痞女 提交于 2020-05-15 05:56:07
问题 I am loading data from a public API after my component is mounted. When the data is loaded I am passing it to the reducer, but it always fires twice. This is what I have: function MyComponent(props) { function reducer(data, action) { switch (action.type) { case 'INITIALIZE': return action.payload; case 'ADD_NEW': const newData = {...data}; newData.info.push({}); return newData; } } const [data, dispatch] = React.useReducer(reducer, null); useEffect(() => { fetch(URL) .then(response => {