Reactjs 从操作重新执行/调用调度

Reactjs 从操作重新执行/调用调度,reactjs,redux,redux-thunk,Reactjs,Redux,Redux Thunk,如何从操作内部分派操作,而不从初始调用传递它?我只想移除道具。分派,但我无法让它工作。 已经在使用redux thunk了,我还没有反应过来。我的版本不错,但我想它不是最好的 <Files onDrop={file => { props.dispatch(addFile(props.dispatch, {id: uuidv4(), file})); }} /> 您可以使用mapDispatchToProps并将其作为第二个参数传递给connec

如何从操作内部分派操作,而不从初始调用传递它?我只想移除
道具。分派
,但我无法让它工作。 已经在使用redux thunk了,我还没有反应过来。我的版本不错,但我想它不是最好的

<Files
    onDrop={file => {
        props.dispatch(addFile(props.dispatch, {id: uuidv4(), file}));
    }}
/>

您可以使用
mapDispatchToProps
并将其作为第二个参数传递给connect方法。 然后你将有派遣方法和你的道具

应该是这样的:

const mapDispathToProps = dispatch => ({
    addFileAction: (file) => addFile(dispatch, file)
})
export default connect(null, mapDispathToProps)(Component);

您的addFile操作现在也可以将分派传递给其他操作。

如果您使用的是
redux thunk
,则您的操作可能如下所示:

// actions.js
export function addFile(id = uuidv4(), file = []) {
  return dispatch => {
    dispatch(doSomething(id));

    dispatch({
        type: 'ADD_FILE',
        payload: {
            id,
            file,
        },
    });
  }
}

function doSomething(id) {
  return dispatch => {
    dispatch(callOtherAction(id));
  }
}

// component.js
import { addFile } from './actions.js';

const YourComponent = ({ addFile }) => <button onClick={() => addFile(id, file)}>Add File</button>;

export default connect(null, { addFile })(YourComponent)
//actions.js
导出函数addFile(id=uuidv4(),file=[]){
返回调度=>{
调度(剂量计(id));
派遣({
键入:“添加_文件”,
有效载荷:{
身份证件
文件
},
});
}
}
功能剂量仪(id){
返回调度=>{
调度(callOtherAction(id));
}
}
//component.js
从“/actions.js”导入{addFile};
constyourcomponent=({addFile})=>addFile(id,file)}>addFile;
导出默认连接(null,{addFile})(YourComponent)

无需将
dispatch
方法作为参数传递。

哦,是的,这太棒了,谢谢!终于开始工作了
mapDispatchToProps
props.addFile({id:uuidv4(),file})成功了。谢谢!
// actions.js
export function addFile(id = uuidv4(), file = []) {
  return dispatch => {
    dispatch(doSomething(id));

    dispatch({
        type: 'ADD_FILE',
        payload: {
            id,
            file,
        },
    });
  }
}

function doSomething(id) {
  return dispatch => {
    dispatch(callOtherAction(id));
  }
}

// component.js
import { addFile } from './actions.js';

const YourComponent = ({ addFile }) => <button onClick={() => addFile(id, file)}>Add File</button>;

export default connect(null, { addFile })(YourComponent)