reactjs,如何在类级别传递道具
这是我试图做的一个例子。我想在MyComponent类中传入props,而不使用reactjs,如何在类级别传递道具,reactjs,redux,react-redux,Reactjs,Redux,React Redux,这是我试图做的一个例子。我想在MyComponent类中传入props,而不使用,因为我正在将MyComponent传递给其他一些库 import React, { Component } from 'react'; class MyComponent extends Component { render() { const { hello } = this.props; return ( <div>{hello}</div> )
,因为我正在将MyComponent传递给其他一些库
import React, { Component } from 'react';
class MyComponent extends Component {
render() {
const { hello } = this.props;
return (
<div>{hello}</div>
);
}
}
// this line is wrong
MyComponent.props = {
hello: 'Hello World!'
}
<MyComponent />
import React,{Component}来自'React';
类MyComponent扩展组件{
render(){
const{hello}=this.props;
返回(
{你好}
);
}
}
//这条线错了
MyComponent.props={
你好:“你好,世界!”
}
react redux connect如何添加this.props.state并将任何其他函数映射到组件的props?我想将自定义对象注入组件。您需要创建一个
您需要创建一个
您可以设置默认道具。我认为这是一个很好的做法
import React, { Component } from 'react';
class MyComponent extends Component {
render() {
const { hello } = this.props;
return (
<div>{hello}</div>
);
}headerProp: "Header from props...",
contentProp:"Content from props..."
}
MyComponent.defaultProps = {
hello: 'Hello World!'
}
<MyComponent />
import React,{Component}来自'React';
类MyComponent扩展组件{
render(){
const{hello}=this.props;
返回(
{你好}
);
}headerProp:“来自道具的头…”,
contentProp:“来自道具的内容…”
}
MyComponent.defaultProps={
你好:“你好,世界!”
}
您可以设置默认道具。我认为这是一个很好的做法
import React, { Component } from 'react';
class MyComponent extends Component {
render() {
const { hello } = this.props;
return (
<div>{hello}</div>
);
}headerProp: "Header from props...",
contentProp:"Content from props..."
}
MyComponent.defaultProps = {
hello: 'Hello World!'
}
<MyComponent />
import React,{Component}来自'React';
类MyComponent扩展组件{
render(){
const{hello}=this.props;
返回(
{你好}
);
}headerProp:“来自道具的头…”,
contentProp:“来自道具的内容…”
}
MyComponent.defaultProps={
你好:“你好,世界!”
}
想想你在问什么。你想要传递道具,而不是传递道具。否:这不是您使用React方法的方式。在您的示例中,只需说让props={…}
,然后使用
,因为这很好:从编程的角度来看,没有理由不想这样做,这实际上是有意义的。请思考一下您的问题。你想要传递道具,而不是传递道具。否:这不是您使用React方法的方式。在您的示例中,只需说let props={…}
,然后使用
,因为这很好:从编程的角度来看,没有理由不想这样做。
import React, { Component } from 'react';
class MyComponent extends Component {
render() {
const { hello } = this.props;
return (
<div>{hello}</div>
);
}headerProp: "Header from props...",
contentProp:"Content from props..."
}
MyComponent.defaultProps = {
hello: 'Hello World!'
}
<MyComponent />