gpt4 book ai didi

reactjs - 我什么时候应该在 React 组件中使用 getInitialState

转载 作者:行者123 更新时间:2023-12-03 13:31:41 27 4
gpt4 key购买 nike

我有一个 React 组件,单击该组件时会切换 className

var Foo = React.createClass({
getInitialState: function() {
return {className: ''}
},

render: function(){
var className = 'bar ' + this.state.className
return React.createElement('div', {className: className, onClick: this.onClick})
},

onClick: function() {
this.setState({className: 'baz'})
}
});

它工作正常,但是当我渲染应用程序服务器端时,出现以下错误

Warning: getInitialState was defined on a component, a plain JavaScript class.
This is only supported for classes created using React.createClass.
Did you mean to define a state property instead?

我的构建步骤是这样设置的

var Foo = require('./Foo');
var factory = React.createFactory(Foo);
module.exports = React.renderToString(factory({}));

为什么我做错了,应该怎么做?

最佳答案

我不确定这是否有帮助,但是在使用 Fluxible 时,这是我作为 require 组件的一部分与 JSX 一起使用的语法

var app = new Fluxible({
component: React.createFactory(require('./Components/startup.react.jsx'))
});

关于reactjs - 我什么时候应该在 React 组件中使用 getInitialState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com