我学习如何使用样式组件的反应,我做了一步一步的视频。我有这段代码,这行和其他代码都有问题
color: ${(props) => (props.invalid ? "red" : "black")} VS Code告诉我
'ThemedStyledProps、"key“、HTMLAttributes> &{. }、any>等类型可能不存在
”属性‘,您的意思是’onInvalid‘?ts(2568)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import React, { useState } from "react";
import styled from "styled-components";
import Button from "../../UI/Button/Button";
import "./CourseInput.css";
const FormControl = styled.div`
margin: 0.5rem 0;
& label {
font-weight: bold;
display: block;
margin-bottom: 0.5rem;
color: ${(props) => (props.invalid ? "red" : "black")}
}
& input {
display: block;
width: 100%;
border: 1px solid ${(props) => (props.invalid ? "red" : "#ccc")};
background: ${(props) => (props.invalid ? "red" : "transparent")}
font: inherit;
line-height: 1.5rem;
padding: 0 0.25rem;
}
& input:focus {
outline: none;
background: #fad0ec;
border-color: #8b005d;
}
`;
const CourseInput = (props) => {
const [enteredValue, setEnteredValue] = useState("");
const [isValid, setIsValid] = useState(true);
const goalInputChangeHandler = (event) => {
//set back true
if (event.target.value.trim().length > 0) {
setIsValid(true);
}
setEnteredValue(event.target.value);
};
const formSubmitHandler = (event) => {
event.preventDefault();
if (enteredValue.trim().length === 0) {
//trim deletes white spacec overall
setIsValid(false);
return;
}
props.onAddGoal(enteredValue);
};
return (
<form onSubmit={formSubmitHandler}>
{/* <div className={`form-control ${!isValid ? "invalid" : ""}`}> */}
<FormControl invalid={!isValid}>
<label>Course Goal</label>
<input type="text" onChange={goalInputChangeHandler} />
</FormControl>
{/* </div> */}
<Button type="submit">Add Goal</Button>
</form>
);
};
export default CourseInput;
发布于 2022-01-21 13:00:16
由于无效不是div元素默认知道的支柱,因此它不会识别它。
要克服这一问题,您可以使用"$“键提供FormControl支持,如下所示:
<FormControl $invalid={!isValid}>然后,在FormControl内部:
color: ${(props) => (props.$invalid ? "red" : "black")}https://stackoverflow.com/questions/70778383
复制相似问题