简体   繁体   English

React-Native:提交时调度不起作用

[英]React-Native: Dispatch on submit is not working

I am trying to do a basic todo list however when I dispatch an action after pressing add it doesn't dispatch.我正在尝试做一个基本的待办事项列表,但是当我在按下添加后调度一个动作时它不会调度。

I've taken the dispatch(todo(todoList)) out of every function and left it in the main ToDo component to do multiple calls on every letter typed into the search box and I can see an update in my redux store in Redux-dev tools so I know my todo works but It won't dispatch() when I try to submit.我已经从每个 function 中取出dispatch(todo(todoList))并将其留在主ToDo组件中,以便对输入到搜索框中的每个字母进行多次调用,我可以在Redux-dev tools中的 redux 存储中看到更新Redux-dev tools ,所以我知道我的todo有效,但是当我尝试提交时它不会dispatch() Please can someone help me.请有人可以帮助我。

This is my code:这是我的代码:

import {useDispatch } from 'react-redux'
import { todo } from './action/todo'

const ToDo = () => {
    const [todo, setTodo] = useState('')
    const [todoList, setTodoList] = useState([])
    const dispatch = useDispatch()
    
    const handleSubmit = (id , todo) => {
        const newTodoList = todoList.concat({ id: id, val: todo })
        return (
            setTodo(''),
            todo.length === 0
                ? <Text/>
                : setTodoList(newTodoList) // if I put the dispatch here it doesn't work either
        )
    }
    return (
        <View style={styles.addPhotoCont}>
            <TextInput
                placeholder={props.textInputPlaceholder}
                onChangeText={props => setTodo(props)}
                value={todo}
            />
            <TouchableOpacity 
                onPress={() => handleSubmit(Date.now(), todo) && dispatch(todo(todoList))}>
                <Text style={styles.addButton}>Add</Text>
            </TouchableOpacity>
        </View>
    )
}

It looks like you set todo twice, once as an import, and the second time as state.看起来您设置了两次todo ,一次作为导入,第二次作为 state。 When you call dispatch and pass in todo it is calling the state version.当您调用 dispatch 并传入todo时,它调用的是 state 版本。

You should put the dispatch in the handleSubmit function.您应该将调度放在handleSubmit中。

Also, looking at the handleSubmit function, the return will not work.还有,看handleSubmit function,return也不行。 You can only return one thing.你只能退回一件事。 You can place the other functions above the return statement.您可以将其他函数放在 return 语句之上。

Edit: Code sample below:编辑:下面的代码示例:

import { useDispatch } from 'react-redux'
import { todo } from './action/todo'

const ToDo = (props) => {
    const [todoInputValue, setTodoInputValue] = useState('')
    const dispatch = useDispatch()
    
    const handleSubmit = (todo) => {
        dispatch(todo({id: Date.now(), val: todoInputValue}))
        setTodoInputValue('')
    }
    
    return (
        <View style={styles.addPhotoCont}>
            <TextInput
                placeholder={props.textInputPlaceholder}
                onChangeText={value => setTodoInputValue(value)}
                value={todoInputValue}
            />
            <TouchableOpacity 
                onPress={() => handleSubmit(Date.now())}>
                <Text style={styles.addButton}>Add</Text>
            </TouchableOpacity>
        </View>
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM