在ReactNative中,您可以执行类似new SomeComponent(props)
和return someComponent.render()
的操作。我猜这是基本上直接声明<SomeComponent someProp="etc"/>
时JSX的作用?如果是这种情况,手动操作组件而不是使用JSX是否有任何潜在的缺点?
答案 0 :(得分:1)
在ReactNative中,你可以做一些像new的事情 SomeComponent(props)并返回someComponent.render()
不是真的。如果您手动实例化组件类,那么生命周期方法('''
my situation is that I know that this relationship is true:
(\sum_{i=1}^n d a_i / dt) ** 2 = f(a).
In this example, I assume a is 2 dimensional, f(a) = a1 **2 - a1 * a2,
and I want to evolve the equation for 5 time steps.
'''
def residual(dadt, current_a):
return (np.sum(dadt) ** 2 - (current_a[0] **2 - np.sum(a) * current_a[0] * current_a[1])) ** 2
'''
My real problem is a little more complicated than this, but the
basic elements are here. Now, my strategy is to attempt to form
something like an Ax=b formulation.
'''
'''
Here is an approach that works. I find a values that make the residual zero.
This approach is a little time-consuming because I have to solve the least-squares
problem after each time iteration.
This is a little goofy because I use an optimization engine to solve the problem.
I think it would be better to cast this as a least squares regression problem.
'''
from scipy.optimize import minimize as mini
n_time_steps = 300
a0 = np.ones(2) * .5
a = a0
dt = 1e-3
a0_record = []
for _ in range(n_time_steps):
a0_record.append(a[0])
dadt = mini(residual, np.ones(a.size), args=(a,)).x
a += dadt * dt
# Let's plot the time history. Maybe that can help verify the new approach.
import matplotlib.pyplot as plt
plt.plot(a0_record)
plt.savefig('a0baseline.pdf')
plt.clf()
'''
I want to cast this into a single least-squares problem. In this case, I think
I should have 2 * 300 = 600 free variables. Since I only have 300 time steps
to enforce the residual to be zero, I think that my system is underdetermined.
I don't quite know how to do this. I sketched my best shot so far below but I
am sure that there is much improvement to make.
'''
a = np.ones((2, n_time_steps))
A = np.stack([residual((a[i] - a[i-1]) / dt, a[i-1]) for i in range(1,n_time_steps)])
xx = np.linalg.lstsq(A, np.zeros(n_time_steps))
,componentWillMount()
))将无法工作。
我猜测这基本上就是JSX直接做的事情 声明
看一下babel编译的内容,但基本上JSX编译成简单的js:
componentDidMount(
转换为:
class Hello extends React.Component {
render() {
return <div>Hello {this.props.toWhat}</div>;
}
}
如果是这种情况,手动是否有任何潜在的缺点 操纵组件而不是使用JSX
您可以使用class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}
并完全避免使用JSX,但根据我的经验,这更难,更冗长。像你最初建议的那样手动实例化组件会破坏使用React的目的:)
一些更详细的好链接: