反思您的问题
我认为,这个错误源于对组件参数传递方式的误解.您认为简单地声明传递给组件的参数就足够了.不是的该组件接收一个包含所有传递的属性的对象,包括在您的情况下名为"值"的属性.因此,如果您继续这样做,您会发现使用value.value
从对象访问value
参数有点尴尬.
<Card value={577} />
// Now "value" is a object, what stored a properties (as "value={577}" attribute)
const Card = (value) => {
// Now can get "value" attribute from "value" object - not readable
console.log(value.value) // 577
}
溶液
在React.js中,组件将传递的变量作为对象接收.您可以直接声明对象(例如,作为props
)并稍后引用其参数,或者直接声明参数,如第二个例子所示.
<Card number={577} anotherProp={true} />
function Card(props) {
// Can use properties as parameter of props object
console.log(props.number) // 577
console.log(props.anotherProp) // true
}
// or
function Card({ number, anotherProp }) {
// Can use properties when directly declared them in function
// That's called "Destructuring assignment"
console.log(number) // 577
console.log(anotherProp) // true
}
更多信息
例如
// Import the React and ReactDOM modules
const { useState, useEffect } = React;
// The component receives the passed values as object (props)
function Card(props) {
const digits = `${props.number}`.split('');
return (
<div style={{ display: "flex" }}>
{digits.map((val) => (
<div
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
}
// Instead of declaring the object (props), you can directly declare the parameters within { and }
// That's called "Destructuring assignment"
function AnotherCard({ number }) {
const digits = `${number}`.split('');
return (
<div style={{ display: "flex" }}>
{digits.map((val) => (
<div
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
}
// App component
function App() {
return (
<div style={{ display: "grid", gap: "10px" }}>
例如 #1 (Card component with props object)
<Card number={577} />
例如 #2 (AnotherCard component with destructuring assignment)
<AnotherCard number={688} />
</div>
);
}
// Render the application to the DOM
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
<div id="root"></div>
例如 with API
// Import the React and ReactDOM modules
const { useState, useEffect } = React;
// The component receives the passed values as object (props)
function Card(props) {
const digits = `${props.number}`.split('');
return (
<div style={{ display: "flex" }}>
{digits.map((val) => (
<div
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
}
// Instead of declaring the object (props), you can directly declare the parameters within { and }
// That's called "Destructuring assignment"
function AnotherCard({ number }) {
const digits = `${number}`.split('');
return (
<div style={{ display: "flex" }}>
{digits.map((val) => (
<div
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
}
// App component
function App() {
// Declare visitor with default object
const [visitor, setVisitor] = useState({
count: 0,
click: 0
});
// Request data of visitor
useEffect(() => {
fetch('https://count.cab/hit/ghG6Oirn0b')
.then(response => response.json())
.then(v => setVisitor(v));
}, []);
// Will see that the code runs first, and the fetch will update the visitor value asynchronously
console.log(visitor)
return (
<div style={{ display: "grid", gap: "10px" }}>
例如 #1 (Card component with props object)
<Card number={visitor.count} />
例如 #2 (AnotherCard component with destructuring assignment)
<AnotherCard number={visitor.count} />
</div>
);
}
// Render the application to the DOM
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
<div id="root"></div>
例如 with API & extra zero digits
// Import the React and ReactDOM modules
const { useState, useEffect } = React;
// How many digits in total do we want to display
const DIGIT_COUNT = 5
// The component receives the passed values as object (props)
function Card(props) {
let digits = `${props.number}`.split('');
// Determine the number of digits
const numDigits = digits.length;
// If the number has less than DIGIT_COUNT digits, add leading zeros
if (numDigits < DIGIT_COUNT) {
digits = Array(DIGIT_COUNT - numDigits).fill(0).concat(digits);
}
return (
<div style={{ display: "flex" }}>
{digits.map((val, index) => (
<div
key={index}
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
}
// Instead of declaring the object (props), you can directly declare the parameters within { and }
// That's called "Destructuring assignment"
function AnotherCard({ number }) {
let digits = `${number}`.split('');
// Determine the number of digits
const numDigits = digits.length;
// If the number has less than DIGIT_COUNT digits, add leading zeros
if (numDigits < DIGIT_COUNT) {
digits = Array(DIGIT_COUNT - numDigits).fill(0).concat(digits);
}
return (
<div style={{ display: "flex" }}>
{digits.map((val, index) => (
<div
key={index}
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
}
// App component
function App() {
// Declare visitor with default object
const [visitor, setVisitor] = useState({
count: 0,
click: 0
});
// Request data of visitor
useEffect(() => {
fetch('https://count.cab/hit/ghG6Oirn0b')
.then(response => response.json())
.then(v => setVisitor(v));
}, []);
return (
<div style={{ display: "grid", gap: "10px" }}>
例如 #1 (Card component with props object)
<Card number={visitor.count} />
例如 #2 (AnotherCard component with destructuring assignment)
<AnotherCard number={visitor.count} />
</div>
);
}
// Render the application to the DOM
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
<div id="root"></div>