How to change state from outside component #368
Comments
Hi,
But, sometimes it's hard to move state upper in a tree, sometimes we need to encapsulate such logic inside component without affecting other components, and sometimes we just have no time, and even I wrote that it's not good idea, I can't say that it's very bad idea. In recompose the only good way to work with such state is to use mapPropsStream(props$ => props$.scan( /* here logic to update state */ )) |
yes I agree with you, but sometime we need encapsulate the state with component that will make the component expose less api for other users( developers ), so this is why I need put the state into the component scope, right now i have no good idea to handle it. |
I want to propose two ways for this use case: 1. [Simple] Just pass
|
@evenchange4 how should I implement it without Rxjs? do you have some idea |
@dingchaoyan1983 Have you tried to use let counter = 1
console.log(counter++) // log 1
console.log(++counter) // log 3 IMO always writing |
I am going to close this issue now. Feel free to reopen it if you have further questions. |
Greetings const Item = withState('checked', 'setChecked', false)(
({ checked, setChecked }) => (
<Checkbox checked={checked} onChange={({ target }) => setChecked(target.checked)} />
)
)
const App = withState('checkedAll', 'setCheckedAll', false)(
({ checkedAll, setCheckedAll }) => (
<div>
<Checkbox checked={checkedAll} onChange={({ target }) => setCheckedAll(target.checked)} />
<List>
<Item checked={checkedAll} />
<Item checked={checkedAll} />
<Item checked={checkedAll} />
<Item checked={checkedAll} />
<Item checked={checkedAll} />
<Item checked={checkedAll} />
<Item checked={checkedAll} />
</List>
</div>
)
) the problem here is when i try to check all my items it does not work, i have tried with mapPropsStream and componentFromStream according to comments above but it doesn't work. Thanks. |
now I am using recompose to create HOC for my stateless component, and I use
withState
to hold the inner state in the HOC, and this state is initial from the pass in props. this is work fine in the mount period, but I don't know how to handle the state change from props at theupdate
period, i just want to use lifecyclecomponentWillReceiveProps
to handle that, but nothing happened. so anyone can help me on this?the code just like below:
we can see my purpose is to pass a counter the component, and click the button we can change the counter, but we also can change the counter from outside of component, it can also effect the inner value. I don't know how to implement it by using
recompose
. I know we can update state at lifecycle methodcomponentWillReceiveProps
, but seems it hard to write it in recompose wayThe text was updated successfully, but these errors were encountered: