Я пытаюсь запустить функцию действия в компоненте, который создает запрос axios на серверной части.Эта функция (addList) работает в другом компоненте, но когда я пытаюсь использовать ее в другом компоненте, она не работает.
Вот компонент, в котором addList не работает.addList вызывается в функции onRowAdd.
import React, { useState } from "react";
import MaterialTable from "material-table";
import { addList } from '../../actions/profile';
import PropTypes from "prop-types";
import { connect } from "react-redux";
const ListTable = (list, {addList}) => {
const l = list;
const [data, setData] = useState(l.list.list);
console.log(l.list.list);
list = l.list.list;
var listname = l.list.name;
const handlebClick = (e) => {
e.preventDefault();
console.log({list});
};
return (
<div className="list-table">
<button onClick={e => handlebClick(e)}>Apply Sequence</button>
<MaterialTable
title={listname}
columns={[
{ title: "User Name", field: "username" },
{
title: "Link",
field: "link",
initialEditValue: ""
},
{ title: "Name", field: "name", initialEditValue: "" },
{ title: "Source", field: "source", initialEditValue: "" },
{ title: "Date Added", field: "dateAdded", type: "date" },
{ title: "Notes", field: "notes", initialEditValue: "" }
]}
data={l.list.list}
editable={{
onRowAdd: newData =>
new Promise((resolve, reject) => {
setTimeout(() => {
{
const d = data;
d.push(newData);
setData({ d });
console.log(data);
addList(data, l.list.name, true);
resolve();
//put request to change list
}
resolve();
}, 1000);
}),
onRowUpdate: (newData, oldData) =>
new Promise((resolve, reject) => {
setTimeout(() => {
{
const d = data;
//console.log(d, oldData);
const index = d.indexOf(oldData);
d[index] = newData;
setData({ d });
resolve();
//put request to change list
}
resolve();
}, 1000);
}),
onRowDelete: oldData =>
new Promise((resolve, reject) => {
setTimeout(() => {
{
let d = data;
console.log(d);
if (d.length > 0) {
const index = d.indexOf(oldData);
d.splice(index, 1);
setData({ d });
}
resolve();
//put request to change list
}
resolve();
}, 1000);
})
}}
/>
</div>
);
};
ListTable.propTypes = {
addList: PropTypes.func.isRequired
};
export default connect(
null,
{ addList }
)(ListTable);
Вот компонент, в котором работает addList.Вызывается в кнопках отправки по клику.
import React, { Fragment, useState } from "react";
import { Link, withRouter } from "react-router-dom";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { addList } from "../../actions/profile";
function ListItem({ listitem, index }) {
return <div className="listitem">Username: {listitem.username}</div>;
}
function ListForm({ addListItem }) {
const [value, setValue] = useState("");
const handleSubmit = e => {
e.preventDefault();
if (!value) return;
addListItem(value);
setValue("");
};
return (
<form onSubmit={handleSubmit}>
<input
className="input"
type="text"
placeholder="* Username"
name="username"
value={value}
onChange={e => setValue(e.target.value)}
required
/>
</form>
);
}
const AddList = ({ addList }) => {
const [list, setList] = useState([]);
const addListItem = username => {
const newListItems = [...list, { username }];
setList(newListItems);
};
return (
<Fragment>
<h1 className="large text-primary">Add A List</h1>
<p className="lead">
<i className="fas fa-code-branch" /> Add a list of your followers'
usernames
</p>
<div className="form">
<div className="form-group" />
<div className="list">
{list.map((listitem, index) => (
<ListItem key={index} index={index} listitem={listitem} />
))}
<ListForm addListItem={addListItem} />
</div>
<input
type="submit"
className="btn btn-primary my-1"
onClick={e => {
e.preventDefault();
console.log(list);
const listname = prompt(
"Please enter a name for the list",
"List Name"
);
addList([{}], listname, false);
}}
/>
<Link className="btn btn-light my-1" to="/dashboard">
Go Back
</Link>
</div>
</Fragment>
);
};
AddList.propTypes = {
addList: PropTypes.func.isRequired
};
export default connect(
null,
{ addList }
)(withRouter(AddList));
Ошибка Uncaught TypeError: addList не является функцией