0

i am new to reactnative, i was trying to build todo app using useReducer and asyncstorage but not able to achieve that, i have read every article and related questions i am able to do it using useState but did not got the result with useReducer. Any help/suggestions will be appreciated. Thank You .

const MainScreen = () => {
  const getData = async () => {
    try {
      const data = await AsyncStorage.getItem('itemlist');
      return (output = JSON.parse(data));
    } catch (e) {
      
    }
  };

  React.useEffect(() => {
    getData();
  }, []);

  const [text, setText] = React.useState('');// for textinput

  const { dispatch } = useContext(NotesContext);


 
  

  const handleSubmit = async () => {
    try {
      const output = JSON.stringify(state);
      await AsyncStorage.setItem('itemlist', output);
    } catch (error) {
      console.log(error);
    }
    
    if (text) {
      dispatch({ type: 'ADD_TODO', payload: text });
    }
    setText('');
  };
  
  
  //this is my reducer function below
  export const TodoReducer = (state, action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return state.concat({
        task: action.payload,
        id: uuid.v4(),
        complete: false,
      });
    
    case 'DELETE_TODO':
      return state.filter((todo) => todo.id !== action.payload);
    default:
      throw new Error();
  }
};

0 Answers0