javascript-如何在React中记录道具?

前端之家收集整理的这篇文章主要介绍了javascript-如何在React中记录道具? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

React的新手,这只是我上课的第一天.
我要做的就是,当我单击一个框时,记录颜色道具.

我知道我不能做console.log(this.props.color)
因为这是引用应用程序…
现在这真是令人困惑..任何提示将不胜感激.

  1. class Boxes extends Component{
  2. render(props){
  3. return (
  4. <div className="Boxes" onClick={this.props.getBoxColor}>
  5. <div className="Box1" color="red"></div>
  6. <div className="Box2" color="orange"></div>
  7. <div className="Box3" color="yellow"></div>
  8. <div className="Box4" color="green"></div>
  9. <div className="Box5" color="blue"></div>
  10. </div>
  11. );
  12. }
  13. }
  14. class App extends Component {
  15. getBoxColor=()=>{
  16. console.log(this.props)
  17. }
  18. render() {
  19. return (
  20. <Boxes classColor={this.color} getBoxColor={this.getBoxColor} />
  21. )
  22. }
  23. }
  24. ReactDOM.render(<App />,document.getElementById('root'));
最佳答案
试试这个,告诉我它是否适合您.

  1. class Box extends React.Component {
  2. render() {
  3. const className = this.props.className;
  4. const color = this.props.color;
  5. return (
  6. <div
  7. className={className}
  8. color={color}
  9. onClick={() => console.log(color)}
  10. />
  11. );
  12. }
  13. }
  14. class App extends React.Component {
  15. render() {
  16. return (
  17. <div>
  18. <Box className="Box1" color="red" />
  19. <Box className="Box2" color="blue" />
  20. <Box className="Box3" color="green" />
  21. </div>
  22. );
  23. }
  24. }
  25. ReactDOM.render(<App />,document.getElementById("root"));

猜你在找的JavaScript相关文章