我有这个小部件:调用其他组件的方法
import React from 'react';
const Footer = (props) => {
return (
<footer className="top">
<p>{props.items} items in your menu</p>
</footer>
)
}
const updateItems = (n) => {
this.props.items = this.props.items + n;
};
Footer.propTypes = {
items: React.PropTypes.string.isRequired
};
export default Footer;
而且从主要成分:
// All irrelevant imports
import Footer from './Footer';
class App extends React.Component {
// All the irrelevant vars and methods
removeFish = (key) => {
const fishes = {...this.state.fishes};
fishes[key] = null;
this.setState({ fishes });
Footer.updateItems(-1); // <-- This is not updating the items count in the footer component
};
}
我Footer.updateItems似乎并没有更新的价值,甚至控制台火没有错误,应用程序符合,
这将是正确的方法?
'updateItems'应该是Footer组件的一个方法,并更新Footer状态的值,而不是道具。 – Andrew