我有这个React组件。我想在函数test()中更改变量 title 的值。
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ValueStore } from '../../host/redux/list.store';
import { removeLast } from '../../host/redux/list.action';
import { Injector } from '@angular/core';
import { Model } from '../../model';
interface ReactChildComponentViewProps {
title: String;
store: ValueStore;
injector: Injector;
}
var ReactChildComponent = React.createClass<ReactChildComponentViewProps, any>({
getInitialState: function() {
console.log( 'Using Angular 2 injector in React body, model.uuid: ' + model.uuid );
},
render: function() {
var title = "Ttile";
return(
<div>
{title}
</div>
);
}
});
export class ReactChildComponentView {
static initialize(title, store: ValueStore, containerId: string, injector: Injector) {
ReactDOM.render(<ReactChildComponent title={title} store={store} injector={injector}/>, document.getElementById(containerId));
}
static test() {
console.log('Test');
//this.props.store.dispatch( removeLast() );
}
}
是否可以在函数test()中更改title的值。是的我该怎么办?
答案 0 :(得分:0)
据我了解,您想让title
动态化?
您需要将title
置于状态,然后您可以使用this.setState({title:"whatever"});
轻松操作它,为了显示它,您需要编写this.state.title