反应类型。我可以从父母到子部件usestate钩子通过
您好,我有parent组件是应用程序,我有3个儿童组件登录,todoform和todolist 当我试图将挂钩状态传递到登录时,我得到了这个错误 - > login.js:26未被未来的(在承诺中)TypeError:settoken不是一个函数。我不知道为什么我的应用程序中的挂钩在子组件(登录)中不起作用(登录) 在login.js:26:1。
有一个代码
const Login = ({setIslogin,islogin,setUsername,setToken,token,username}) => {
const [login, setLogin] = useState('');
const [password, setPassword] = useState('');
const [userid, setUserid] = useState()
const navigation = useNavigate()
const sign = () => {
Axios.post('http://localhost:3001/api/login/', { login: login, password: password, islogin: islogin }).then((result) => {
const user = result.data;
if (user.islogin == true) {
setUserid(user.id)
setToken(user.token)
window.localStorage.setItem("json token", user.token);
setUsername(login)
setIslogin(true)
}
else {
console.log("nie udalo sie zalgoowac")
}
})
}
if (islogin === true) {
return (
<App userid= {userid} />
)
} else {
return (
<div className="App">
<Header />
<section class="landing-page">
<div class="form-container">
<form>
<h1>Login</h1>
<label>Login</label>
<input type="text" onChange={(e) => { setLogin(e.target.value) }} />
<label>Password</label>
<input type="password" onChange={(e) => { setPassword(e.target.value) }} />
<button type="button" onClick={sign}>Login</button>
</form>
</div>
</section>
</div>
);
}
};
export default Login;
And the App component - >
function App() {
const [todos, setTodos] = useState([]);
const [input, setInput] = useState('');
const [tasks,setTasks] = useState([]);
const [islogin,setIslogin] = useState(false);
const [username,setUsername] =useState('')
const [userid, setUserid] = useState('')
const [token, setToken] = useState('');
const { state } = useLocation();
if (islogin===false){
return(<Login setIslogin={setIslogin} setUsername={setUsername} setToken={setToken} token={token} username={username}/>)
}
else{
return (
<div className="App">
<header>
<nav>
<li><Link to="/">Home </Link> </li>
<li><Link to="/login">Login </Link></li>
</nav>
<div class="img-header">
{/* <img src={require('./logo.png')} /> */}
</div>
</header>
<section class="landing-page">
<Form setInput={setInput} input={input} username={username} islogin={islogin} id={userid} tasks={tasks} setTasks={setTasks} />
<div class="list-container">
<Todolist setInput={setInput} input={input} username={username} islogin={islogin} id={userid} tasks={tasks} setTasks={setTasks} todos={todos} setTodos={setTodos}/>
</div>
</section>
</div>
);
}
}
export default App;
Hello i have parent component which is App , i have 3 child componets Login, TodoForm and TodoList
When im trying to pass hook state to login i got this err - > Login.js:26 Uncaught (in promise) TypeError: setToken is not a function. I dont have idea why my hooks from App doesnt work in child component(Login)
at Login.js:26:1.
There is a code
const Login = ({setIslogin,islogin,setUsername,setToken,token,username}) => {
const [login, setLogin] = useState('');
const [password, setPassword] = useState('');
const [userid, setUserid] = useState()
const navigation = useNavigate()
const sign = () => {
Axios.post('http://localhost:3001/api/login/', { login: login, password: password, islogin: islogin }).then((result) => {
const user = result.data;
if (user.islogin == true) {
setUserid(user.id)
setToken(user.token)
window.localStorage.setItem("json token", user.token);
setUsername(login)
setIslogin(true)
}
else {
console.log("nie udalo sie zalgoowac")
}
})
}
if (islogin === true) {
return (
<App userid= {userid} />
)
} else {
return (
<div className="App">
<Header />
<section class="landing-page">
<div class="form-container">
<form>
<h1>Login</h1>
<label>Login</label>
<input type="text" onChange={(e) => { setLogin(e.target.value) }} />
<label>Password</label>
<input type="password" onChange={(e) => { setPassword(e.target.value) }} />
<button type="button" onClick={sign}>Login</button>
</form>
</div>
</section>
</div>
);
}
};
export default Login;
And the App component - >
function App() {
const [todos, setTodos] = useState([]);
const [input, setInput] = useState('');
const [tasks,setTasks] = useState([]);
const [islogin,setIslogin] = useState(false);
const [username,setUsername] =useState('')
const [userid, setUserid] = useState('')
const [token, setToken] = useState('');
const { state } = useLocation();
if (islogin===false){
return(<Login setIslogin={setIslogin} setUsername={setUsername} setToken={setToken} token={token} username={username}/>)
}
else{
return (
<div className="App">
<header>
<nav>
<li><Link to="/">Home </Link> </li>
<li><Link to="/login">Login </Link></li>
</nav>
<div class="img-header">
{/* <img src={require('./logo.png')} /> */}
</div>
</header>
<section class="landing-page">
<Form setInput={setInput} input={input} username={username} islogin={islogin} id={userid} tasks={tasks} setTasks={setTasks} />
<div class="list-container">
<Todolist setInput={setInput} input={input} username={username} islogin={islogin} id={userid} tasks={tasks} setTasks={setTasks} todos={todos} setTodos={setTodos}/>
</div>
</section>
</div>
);
}
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论