FormidableLabs / freactal

Clean and robust state management for React and React-like libs.
MIT License
1.65k stars 46 forks source link

Can't get the current state new value after effect #68

Open ralphievolt opened 7 years ago

ralphievolt commented 7 years ago

I have read some of the issues posted here but can't seems to understand it clearly so I decided to create it myself. Here's my code

<Form onSubmit={_handleSearching()}>
  <Form.Input
      type="text"
      id="barcode"
      icon="search"
      placeholder="Search barcode..."
   />
</Form>

Current state value will appear here and will update each form submission

<Step
    icon="inbox"
    title={"Indibox : " + state.indibar}
    description="date processed: "
 />

However getting the state a value is behind like this doing a console.log

    const _handleSearching = event => {
       event.preventDefault();
       const barcode = document.getElementById("barcode").value; //Z09J43H

      effects.readBarcode(barcode);
      console.log(state.indibar);
    };

My state wrapper

const stateWrapperIndibox = provideState({
  initialState: () => ({ indibar: "val" }),
  effects: {
    readBarcode: (effects, newVal) => state =>
      Object.assign({}, state, { indibar: newVal })
  }
});

Thanks

Jascha-Sundaresan commented 7 years ago

maybe try

effects.readBarcode(barcode).then( () => console.log(state.indibar))
divmain commented 6 years ago

Hi @ralphievolt, it looks like you're invoking your event handler immediately. Do things work differently if you pass _handleSearching vs _handleSearching()?